Angular upload pictures to a distributed file server FastDFS

Steps for usage

  1, upload and download the required dependencies

  2, springmvc multimedia parser configured and loaded

Copy the code
    <! - Display Configuration Parser -> 
    <the bean ID = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <! - File Format -> 
        <Property name = "defaultEncoding" = value "UTF-. 8"> </ Property> 
        <-! set the maximum file upload 5MB,. 5 * 1024 1024 * -> 
        <Property name = "maxUploadSize" value = "5242880"> </ Property> 
    </ the bean> 

// application.properties scan file 
<context: property-placeholder location = "classpath: config / application.properties" />
Copy the code
application.properties profile
Data //application.properties file 
FILE_SERVER_URL = http: //192.168.200.128/

 3, the front page of the file upload specification

Copy the code
= function this.uploadFile () { 
        // data is transmitted to the background: 
        var = formData the FormData new new (); 
        // add to the data formData: 
        formData.append ( "File", file.files [0]); 
        return $ HTTP ({ 
            Method,: 'POST', 
            url: '../ the Upload / uploadFile.do', 
            the Data: formData, 
            // add headers, your browser will help us Content-Type is set to multipart / form-the Data ,. 
            headers : { 'the Type-the Content': undefined}, // the Content-the Type: text / text HTML / Plain 
            transformRequest: angular.identity 
        }); 
    }
Copy the code

  4, interface code Background

Copy the code
@RestController 
@RequestMapping ( "/ Upload") 
public class UploadController { 
    @Value ( "FILE_SERVER_URL $ {}") // spring from the container to the service address fastDFS 
    Private String FILE_SERVER_URL; 
    @RequestMapping ( "/ the uploadFile") 
    public the Result uploadFile (MultipartFile file) { 
        the try { 
            String filename = file.getOriginalFilename (); 
            // get the file name suffix 
            String extname = filename.substring (filename.indexOf () + 1 "1."); 
            // compile-time anomalies, try catch capture with 
            FastDFSClient fastDFSClient new new FastDFSClient = ( "CLASSPATH: fastdfs / fdfs_client.conf"); 
            // address of the server storage path 
            String s = fastDFSClient.uploadFile (file.getBytes(), extName);
            // ip address the storage address of the virtual machine, and return the file returned by the server to the front end 
            String URL = S + FILE_SERVER_URL; 
            return the Result new new (to true, URL); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
            return new Result (false, "upload failed"); 
        } 
    } 
}
Copy the code

  5, the top package FastDFS I will become a tool class

Copy the code
public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;
    
    public FastDFSClient(String conf) throws Exception {
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }
    
    / ** 
     * Upload file method 
     * <the p-> the Title: uploadFile </ the p-> 
     * <the p-> the Description: </ the p-> 
     * @param fileName the full path to the file 
     * @param extName file extension does not include (.) 
     * @ param metas file extent information 
     * @return 
     * @throws Exception 
     * / 
    public String the uploadFile (fileName String, String extname, of NameValuePairs [] Metas) throws Exception { 
        String Result = storageClient.upload_file1 (fileName, extname, Metas); 
        return Result; 
    } 
    
    String the uploadFile public (String fileName) throws Exception { 
        return the uploadFile (fileName, null, null); 
    }
    
    String the uploadFile public (fileName String, String extname) throws Exception { 
        return the uploadFile (fileName, extname, null); 
    } 

    / ** 
     * upload method 
     * <P> the Title: the uploadFile </ P> 
     * <P> the Description: </ P> 
     * @param fileContent content file, a byte array 
     * @param extName file extension 
     * @param metas file extent information 
     * @return 
     * @throws Exception 
     * / 
    public String the uploadFile (byte [] fileContent, String extname, of NameValuePairs [ ] Metas) throws Exception { 

        String Result = storageClient.upload_file1 (fileContent, extname, Metas); 
        return Result; 
    }

    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    public String uploadFile(byte[] fileContent, String extName) throws Exception {
        return uploadFile(fileContent, extName, null);
    }
Copy the code

  6, the integration of good background url passed to the front, through the front

Copy the code
$ scope.uploadFile = function () { 
        method // Call uploadService the complete file upload 
        uploadService.uploadFile (). Success (function (the Response) { 
            IF (response.success) { 
                // successful get url, not bombs window $ scope.image_entity.url = response.message; 
            } the else { 
                // fails pop 
                Alert (response.message); 
            } 
        }); 
    }
                
Copy the code

 

 To show our photos to upload.

  1, upload and download the required dependencies

  2, springmvc multimedia parser configured and loaded

Copy the code
    <! - Display Configuration Parser -> 
    <the bean ID = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <! - File Format -> 
        <Property name = "defaultEncoding" = value "UTF-. 8"> </ Property> 
        <-! set the maximum file upload 5MB,. 5 * 1024 1024 * -> 
        <Property name = "maxUploadSize" value = "5242880"> </ Property> 
    </ the bean> 

// application.properties scan file 
<context: property-placeholder location = "classpath: config / application.properties" />
Copy the code
application.properties profile
Data //application.properties file 
FILE_SERVER_URL = http: //192.168.200.128/

 3, the front page of the file upload specification

Copy the code
= function this.uploadFile () { 
        // data is transmitted to the background: 
        var = formData the FormData new new (); 
        // add to the data formData: 
        formData.append ( "File", file.files [0]); 
        return $ HTTP ({ 
            Method,: 'POST', 
            url: '../ the Upload / uploadFile.do', 
            the Data: formData, 
            // add headers, your browser will help us Content-Type is set to multipart / form-the Data ,. 
            headers : { 'the Type-the Content': undefined}, // the Content-the Type: text / text HTML / Plain 
            transformRequest: angular.identity 
        }); 
    }
Copy the code

  4, interface code Background

Copy the code
@RestController 
@RequestMapping ( "/ Upload") 
public class UploadController { 
    @Value ( "FILE_SERVER_URL $ {}") // spring from the container to the service address fastDFS 
    Private String FILE_SERVER_URL; 
    @RequestMapping ( "/ the uploadFile") 
    public the Result uploadFile (MultipartFile file) { 
        the try { 
            String filename = file.getOriginalFilename (); 
            // get the file name suffix 
            String extname = filename.substring (filename.indexOf () + 1 "1."); 
            // compile-time anomalies, try catch capture with 
            FastDFSClient fastDFSClient new new FastDFSClient = ( "CLASSPATH: fastdfs / fdfs_client.conf"); 
            // address of the server storage path 
            String s = fastDFSClient.uploadFile (file.getBytes(), extName); 
            // ip address the storage address of the virtual machine, and return the file returned by the server to the front end
            String URL = S + FILE_SERVER_URL; 
            return the Result new new (to true, URL); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
            return new Result (false, "upload failed"); 
        } 
    } 
}
Copy the code

  5, the top package FastDFS I will become a tool class

Copy the code
public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;
    
    public FastDFSClient(String conf) throws Exception {
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }
    
    / ** 
     * Upload file method 
     * <the p-> the Title: uploadFile </ the p-> 
     * <the p-> the Description: </ the p-> 
     * @param fileName the full path to the file 
     * @param extName file extension does not include (.) 
     * @ param metas file extent information 
     * @return 
     * @throws Exception 
     * / 
    public String the uploadFile (fileName String, String extname, of NameValuePairs [] Metas) throws Exception { 
        String Result = storageClient.upload_file1 (fileName, extname, Metas); 
        return Result; 
    } 
    
    String the uploadFile public (String fileName) throws Exception { 
        return the uploadFile (fileName, null, null); 
    }
    
    String the uploadFile public (fileName String, String extname) throws Exception { 
        return the uploadFile (fileName, extname, null); 
    } 

    / ** 
     * upload method 
     * <P> the Title: the uploadFile </ P> 
     * <P> the Description: </ P> 
     * @param fileContent content file, a byte array 
     * @param extName file extension 
     * @param metas file extent information 
     * @return 
     * @throws Exception 
     * / 
    public String the uploadFile (byte [] fileContent, String extname, of NameValuePairs [ ] Metas) throws Exception { 

        String Result = storageClient.upload_file1 (fileContent, extname, Metas); 
        return Result; 
    }

    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    public String uploadFile(byte[] fileContent, String extName) throws Exception {
        return uploadFile(fileContent, extName, null);
    }
Copy the code

  6, the integration of good background url passed to the front, through the front

Copy the code
$ scope.uploadFile = function () { 
        method // Call uploadService the complete file upload 
        uploadService.uploadFile (). Success (function (the Response) { 
            IF (response.success) { 
                // successful get url, not bombs window $ scope.image_entity.url = response.message; 
            } the else { 
                // fails pop 
                Alert (response.message); 
            } 
        }); 
    }
                
Copy the code

 

 To show our photos to upload.

Guess you like

Origin www.cnblogs.com/wangju/p/11886979.html