For file upload service (unified process files and images) by Fastdfs

1, a simple file upload process analysis chart:

 

 

2, Fastdfs description:

  Fastdfs consists of two roles:

    Tracker (cluster): scheduling (there to help you find a free Storage)

    Storage (cluster): File Storage (help you save a file or to obtain the required documents)

  Process:

    1.Storage tracker and sending the heartbeat connection.

    2. The client request tracker, tracker scheduling a Storage, Storage returns the ip and port.

    3. The client requests Storage, upload files.

    4.Storage save files, generate file_id, and return.

    5. The client receives file_id and save.

3, uploading and downloading flow chart:

       Upload:

 

 

       download:

 

 

 

4, fastdfs upload specific implementation steps:

  1. Fastdfs build a good environment on the server: This operation by the operation and maintenance personnel, developers do not care.

  2. Introducing local maven repository fastdfs jar package (the package is not in the jar maven central warehouse, not automatically downloaded via idea, only manually into the local repository to)

  jar包下载地址(fastdfs_client_v1.20.jar):https://sourceforge.net/projects/fastdfs/files/Java%20Client%20API%20Library/Java%20Client%20Library%20V1.20%20%28compiled%20by%20JDK%201.6%29/fastdfs_client_v1.20.jar/download?use_mirror=liquidtelecom&download=&failedmirror=jaist.dl.sourceforge.net

  Import local repository:

         在jar包所在的目录打开cmd,然后输入以下命令,执行后即可在maven本地仓库导入jar包并在cmd显示出jar包在仓库中的路径。

mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs  -Dversion=1.2 -Dpackaging=jar -Dfile= fastdfs_client_v1.20.jar

 

  3. 项目中引入

<dependency>
        <groupId>org.csource.fastdfs</groupId>
        <artifactId>fastdfs</artifactId>
        <version>1.2</version>
</dependency>

  

  4、项目中引入以下工具类:

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDfsApiOpr {
     
    public static String CONF_FILENAME  = FastDfsApiOpr.class.getResource("/fdfs_client.conf").getFile();

    /**
     * 上传文件
     * @param file
     * @param extName
     * @return
     */
    public static  String upload(byte[] file,String extName) {
 
        try { 
            ClientGlobal.init(CONF_FILENAME);
 
            TrackerClient tracker = new TrackerClient(); 
            TrackerServer trackerServer = tracker.getConnection(); 
            StorageServer storageServer = null;
 
            StorageClient storageClient = new StorageClient(trackerServer, storageServer); 
            NameValuePair nvp [] = new NameValuePair[]{
                    new NameValuePair("age", "18"), 
                    new NameValuePair("sex", "male") 
            }; 
            String fileIds[] = storageClient.upload_file(file,extName,nvp);
             
            System.out.println(fileIds.length); 
            System.out.println("组名:" + fileIds[0]); 
            System.out.println("路径: " + fileIds[1]);
            return  "/"+fileIds[0]+"/"+fileIds[1];
 
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    /**
     * 下载文件
     * @param groupName
     * @param fileName
     * @return
     */
    public static byte[] download(String groupName,String fileName) {
        try {
 
            ClientGlobal.init(CONF_FILENAME);
 
            TrackerClient tracker = new TrackerClient(); 
            TrackerServer trackerServer = tracker.getConnection(); 
            StorageServer storageServer = null;
 
            StorageClient storageClient = new StorageClient(trackerServer, storageServer); 
            byte[] b = storageClient.download_file(groupName, fileName);
            return  b;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        } 
    }
     
//    @Test
//    public void testGetFileInfo(){
//        try {
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            TrackerServer trackerServer = tracker.getConnection();
//            StorageServer storageServer = null;
//
//            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//            FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");
//            System.out.println(fi.getSourceIpAddr());
//            System.out.println(fi.getFileSize());
//            System.out.println(fi.getCreateTimestamp());
//            System.out.println(fi.getCrc32());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
     
//    @Test
//    public void testGetFileMate(){
//        try {
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            TrackerServer trackerServer = tracker.getConnection();
//            StorageServer storageServer = null;
//
//            StorageClient storageClient = new StorageClient(trackerServer,
//                    storageServer);
//            NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");
//            for(NameValuePair nvp : nvps){
//                System.out.println(nvp.getName() + ":" + nvp.getValue());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 删除文件
     * @param groupName
     * @param fileName
     */
    public static void delete(String groupName,String fileName){
        try { 
            ClientGlobal.init(CONF_FILENAME);
 
            TrackerClient tracker = new TrackerClient(); 
            TrackerServer trackerServer = tracker.getConnection(); 
            StorageServer storageServer = null;
 
            StorageClient storageClient = new StorageClient(trackerServer, 
                    storageServer); 
            int i = storageClient.delete_file(groupName,fileName);
            System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException("删除异常,"+e.getMessage());
        } 
    }
}

 

  5、在contorller中使用:

@RestController
public class FileController {

    /**
     * 文件上传  MultipartFile 是SpringMVC封装好的API方便操作上传的文件的
     * @param file
     * @return
     */
    @PostMapping("/fastdfs")
    public AjaxResult upload(MultipartFile file){

        try {
            //获取文件的扩展名
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".")+1);
            String file_id = FastDfsApiOpr.upload(file.getBytes(), extName);
            return AjaxResult.me().setSuccess(true).setMessage("上传成功!").setRestObj(file_id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败!"+e.getMessage());
        }


    }

    /**
     * 删除文件
     * @param file_id
     * @return
     */
    @DeleteMapping("/fastdfs")
    public AjaxResult delete(String file_id){
        try {
            //     /group1/M00/00/01/rBACgF1euQiAEdrcAACbjnUEnrE193.jpg

            file_id = file_id.substring(1);//   group1/M00/00/01/rBACgF1euQiAEdrcAACbjnUEnrE193.jpg

            String groupName = file_id.substring(0, file_id.indexOf("/"));


            String fileName = file_id.substring(file_id.indexOf("/")+1);

            FastDfsApiOpr.delete(groupName, fileName);

            return AjaxResult.me().setSuccess(true).setMessage("删除成功!");

        } catch (Exception e) {
            e.printStackTrace();

            return AjaxResult.me().setSuccess(false).setMessage("删除失败!");

        }


    }


}

 

  6、前端页面代码

          <el-form-item label="logo">
                    <el-upload
                            class="upload-demo"
                            action="http://localhost:6001/services/common/fastdfs"
                            :file-list="addForm.logoList"
                            list-type="picture"
                            :on-success="logoUploadSuccess"
                            :before-upload="handleBeforeUpload"
                            :on-remove="handleLogoRemove">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
methods: {
            handleLogoRemove(file, fileList){
                //file 删除的文件
                //调用删除文件的接口
                let file_id = file.response.restObj
                this.$http.delete("/common/fastdfs?file_id="+file_id)
                    .then(res=>{
                        let {success,message,restObj} = res.data;
                        if(success){
                            //fileList 删除后的文件列表
                            this.addForm.logoList = fileList;
                        }else{
                            this.$message({
                                message:message,
                                type:"error"
                            })
                        }
                    })
            },
            //图片上传之前的回调
            handleBeforeUpload(file){
                if(this.addForm.logoList.length>0){
                    this.$message({
                        message:"只能上传一张图片!",
                        type:"warning"
                    })
                    return false;
                }
            },
            //图片上传成功的回调
            logoUploadSuccess(response, file, fileList){
                //fileList = [{response:{success:true,restObj:''}},file,file]
                this.addForm.logoList = fileList;
                console.log(response)
            }
}

 

Guess you like

Origin www.cnblogs.com/wanghj-15/p/11402962.html