Browse Source

文件上传

Shi Kejing 4 years ago
parent
commit
f8123c20f9

+ 6 - 0
guns-main/src/main/java/cn/stylefeng/guns/zjxl/cnotroller/GcFileUploadController.java

@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.MediaType;
@ -33,6 +35,8 @@ import java.io.IOException;
@Api(description = "文件上传")
@Api(description = "文件上传")
public class GcFileUploadController {
public class GcFileUploadController {
    private Logger logger = (Logger) LoggerFactory.getLogger(GcFileUploadController.class);
    @Autowired
    @Autowired
    FastDFSUtil fastDFSUtil;
    FastDFSUtil fastDFSUtil;
    @Value("${fastDFS.fastdfs_file_url}")
    @Value("${fastDFS.fastdfs_file_url}")
@ -51,6 +55,7 @@ public class GcFileUploadController {
            @ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file
            @ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file
    ) {
    ) {
        try {
        try {
            logger.info("单文件上传file:"+file);
            UploadModel uploadModel = fileUpload(file);
            UploadModel uploadModel = fileUpload(file);
            return new ResultOneModel<UploadModel>(uploadModel);
            return new ResultOneModel<UploadModel>(uploadModel);
        } catch (Exception e) {
        } catch (Exception e) {
@ -72,6 +77,7 @@ public class GcFileUploadController {
        String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
        String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
        String fileName = fullName.substring(0, fullName.lastIndexOf("."));
        String fileName = fullName.substring(0, fullName.lastIndexOf("."));
        //上传到fastdfs
        //上传到fastdfs
        logger.info("GcFileUploadController→mf==="+mf);
        ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(), fileType, "");
        ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(), fileType, "");
        //解析返回的objectNode
        //解析返回的objectNode
        uploadModel.setFileName(fileName);
        uploadModel.setFileName(fileName);

+ 39 - 5
guns-main/src/main/java/cn/stylefeng/guns/zjxlUtil/FastDFSClientPool.java

@ -5,7 +5,10 @@ import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Map.Entry;
@ -15,26 +18,56 @@ import java.util.Map.Entry;
 */
 */
public class FastDFSClientPool {
public class FastDFSClientPool {
    private static volatile FastDFSClientPool pool;
    private static volatile FastDFSClientPool pool;
    private Logger logger = (Logger) LoggerFactory.getLogger(FastDFSClientPool.class);
    private Map<StorageClient, Boolean> map;
    private Map<StorageClient, Boolean> map;
    private int maxPoolSize = 20;
    private int maxPoolSize = 20;
    private int waitTime = 500;
    private int waitTime = 500;
    private int initPoolSize = 5;
    private int initPoolSize = 5;
    static {
    {
        try {
        try {
            
            
            String basePath = FastDFSClientPool.class.getResource("/").getPath();
            String basePath = FastDFSClientPool.class.getResource("/").getPath();
           
            ClientGlobal.init(FastDFSUtil.class.getResource("/config/fdfs_client.conf").getPath());
            ClientGlobal.init("/data/guns-separation/config/fdfs_client.conf");
            System.out.println("FastDFSClientPool→fdfs_client.conf  ===  ");
        } catch (Exception e) {
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
        }
        }
    }
    }
    public  String readfile(String file){
        InputStream stream = this.getClass().getResourceAsStream(file);
        StringBuffer sb = new StringBuffer() ;
        BufferedReader br = null ;
        try {
            br = new BufferedReader(new InputStreamReader(stream,"UTF-8")) ;
            String s=null ;
            while((s=br.readLine()) !=null){
                sb.append(s) ;
            }
            br.close();
        } catch (FileNotFoundException e) {
            logger.error("FileNotFoundException:"+e);
        } catch (IOException e) {
            logger.error("IOException :"+e);
        } finally {
            if(br !=null){
                try {
                    br.close();
                } catch (IOException e) {
                    logger.error("close br error:"+e);
                }
            }
        }
        return sb.toString();
    }
    private FastDFSClientPool() {
    private FastDFSClientPool() {
        init();
        init();
@ -81,8 +114,9 @@ public class FastDFSClientPool {
    private StorageClient getNewStorageClient() {
    private StorageClient getNewStorageClient() {
        try {
        try {
            TrackerClient tracker = new TrackerClient();
            TrackerClient tracker = new TrackerClient();
            logger.info("FastDFSClientPool→tracker==="+tracker);
            TrackerServer trackerServer = tracker.getConnection();
            TrackerServer trackerServer = tracker.getConnection();
            logger.info("FastDFSClientPool→trackerServer   ===   "+trackerServer);
            StorageClient client = new StorageClient(trackerServer, null);
            StorageClient client = new StorageClient(trackerServer, null);
            return client;
            return client;
        } catch (Exception e) {
        } catch (Exception e) {

+ 4 - 1
guns-main/src/main/java/cn/stylefeng/guns/zjxlUtil/FastDFSUtil.java

@ -15,6 +15,8 @@ import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerServer;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
/**
 * FastDFS 客户端工具.
 * FastDFS 客户端工具.
@ -27,7 +29,7 @@ public class FastDFSUtil {
    public final static String FileIdField = "fid";
    public final static String FileIdField = "fid";
    public final static String FileUrlField = "fileUrl";
    public final static String FileUrlField = "fileUrl";
//    static TrackerClient tracker;
    //    static TrackerClient tracker;
//    static TrackerServer trackerServer;
//    static TrackerServer trackerServer;
//    static StorageServer storageServer;
//    static StorageServer storageServer;
//    static StorageClient client;
//    static StorageClient client;
@ -83,6 +85,7 @@ public class FastDFSUtil {
     */
     */
    public static ObjectNode upload(InputStream in, String fileExtension,
    public static ObjectNode upload(InputStream in, String fileExtension,
                                    String description) throws Exception {
                                    String description) throws Exception {
        System.out.println("FastDFSUtil→InputStream === "+in);
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
        try {
            NameValuePair[] fileMetaData;
            NameValuePair[] fileMetaData;