java110 преди 3 години
родител
ревизия
95bde8f16b

+ 20 - 3
java110-core/src/main/java/com/java110/core/client/CosUploadTemplate.java

@@ -65,7 +65,7 @@ public class CosUploadTemplate {
             byte[] context = Base64Convert.base64ToByte(imageBase64);
             is = new ByteArrayInputStream(context);
 
-            COSUtil.uploadByInputStream(cosClient, is,  ftpPath +urlPath);
+            COSUtil.uploadByInputStream(cosClient, is, ftpPath + urlPath);
         } catch (Exception e) {
             logger.error("上传文件失败", e);
             throw new IllegalArgumentException("上传文件失败");
@@ -110,14 +110,15 @@ public class CosUploadTemplate {
         }
         return fileName;
     }
+
     /*
      *文件上传工具方法
      */
-    public String upload(InputStream inputStream,  String ftpPath) {
+    public String upload(InputStream inputStream, String ftpPath) {
         COSClient cosClient = null;
         try {
             cosClient = COSUtil.getCOSClient();
-            COSUtil.uploadByInputStream(cosClient, inputStream, ftpPath );
+            COSUtil.uploadByInputStream(cosClient, inputStream, ftpPath);
         } catch (Exception e) {
             // logger.error("上传文件失败", e);
             throw new IllegalArgumentException("上传文件失败");
@@ -257,4 +258,20 @@ public class CosUploadTemplate {
         return null;
     }
 
+    public InputStream download(String fileName) {
+        COSClient ossClient = null;
+        InputStream is = null;
+        try {
+            ossClient = COSUtil.getCOSClient();
+            is = COSUtil.getInputStreamByCOS(ossClient, fileName);
+            if (null == is) {
+                throw new FileNotFoundException(fileName);
+            }
+
+        } catch (Exception e) {
+            logger.error("ftp通过文件名称获取远程文件流", e);
+        }
+        return is;
+    }
+
 }

+ 26 - 2
java110-core/src/main/java/com/java110/core/client/FileUploadTemplate.java

@@ -47,8 +47,32 @@ public class FileUploadTemplate {
                     ftpPort, ftpUserName,
                     ftpUserPassword, newfileName);
         }
+        return newfileName;
+    }
+
+    public InputStream downloadFile(String fileName){
+
+        String newfileName = ROOT_PATH+ fileName;
+
+        InputStream inputStream = null;
+
+        String ossSwitch = MappingCache.getValue(OSSUtil.DOMAIN, OSSUtil.OSS_SWITCH);
+
+        if (OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) {
+            inputStream = ossUploadTemplate.download(newfileName);
+        } else if (COSUtil.COS_SWITCH_COS.equals(ossSwitch)) {
+            inputStream = cosUploadTemplate.download(newfileName);
+        } else {
+            String ftpServer = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER);
+            int ftpPort = Integer.parseInt(MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT));
+            String ftpUserName = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME);
+            String ftpUserPassword = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERPASSWORD);
+
+            inputStream = ftpUploadTemplate.download(newfileName, ftpServer,
+                    ftpPort, ftpUserName,
+                    ftpUserPassword);
+        }
 
-        String imgUrl = MappingCache.getValue("IMG_PATH");
-        return imgUrl+fileName;
+        return inputStream;
     }
 }

+ 37 - 0
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java

@@ -402,4 +402,41 @@ public class FtpUploadTemplate {
             return false;
         }
     }
+
+
+    public InputStream download(String fileName, String server, int port, String userName, String userPassword) {
+        InputStream is = null;
+        ByteArrayOutputStream bos = null;
+        ByteArrayInputStream fis = null;
+        FTPClient ftpClient = new FTPClient();
+        try {
+            if (!ftpClient.isConnected()) {
+                ftpClient.connect(server, port);
+            }
+            ftpClient.login(userName, userPassword);
+            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
+            ftpClient.enterLocalPassiveMode();
+            int reply = ftpClient.getReplyCode();
+            if (!FTPReply.isPositiveCompletion(reply)) {
+                ftpClient.disconnect();
+            }
+            String f = new String(
+                    (fileName).getBytes("GBK"),
+                    FTP.DEFAULT_CONTROL_ENCODING);
+            is = ftpClient.retrieveFileStream(f);// 获取远程ftp上指定文件的InputStream
+            if (null == is) {
+                throw new FileNotFoundException(fileName);
+            }
+
+        } catch (Exception e) {
+            logger.error("ftp通过文件名称获取远程文件流", e);
+        } finally {
+            try {
+                //closeConnect(ftpClient);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
 }

+ 19 - 4
java110-core/src/main/java/com/java110/core/client/OssUploadTemplate.java

@@ -1,12 +1,11 @@
 package com.java110.core.client;
 
 import com.aliyun.oss.OSSClient;
-import com.java110.utils.cache.MappingCache;
+import com.java110.core.log.LoggerFactory;
 import com.java110.utils.util.Base64Convert;
 import com.java110.utils.util.DateUtil;
 import com.java110.utils.util.OSSUtil;
 import org.slf4j.Logger;
-import com.java110.core.log.LoggerFactory;
 import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -65,7 +64,7 @@ public class OssUploadTemplate {
             byte[] context = Base64Convert.base64ToByte(imageBase64);
             is = new ByteArrayInputStream(context);
 
-            OSSUtil.uploadByInputStream(ossClient, is,  ftpPath + urlPath);
+            OSSUtil.uploadByInputStream(ossClient, is, ftpPath + urlPath);
         } catch (Exception e) {
             logger.error("上传文件失败", e);
             throw new IllegalArgumentException("上传文件失败");
@@ -111,7 +110,7 @@ public class OssUploadTemplate {
         return fileName;
     }
 
-    public String upload(InputStream inputStream,  String ftpPath) {
+    public String upload(InputStream inputStream, String ftpPath) {
         OSSClient ossClient = null;
         try {
             ossClient = OSSUtil.getOSSClient();
@@ -254,4 +253,20 @@ public class OssUploadTemplate {
         return null;
     }
 
+
+    public InputStream download(String fileName) {
+        OSSClient ossClient = null;
+        InputStream is = null;
+        try {
+            ossClient = OSSUtil.getOSSClient();
+            is = OSSUtil.getInputStreamByOSS(ossClient, fileName);
+            if (null == is) {
+                throw new FileNotFoundException(fileName);
+            }
+        } catch (Exception e) {
+            logger.error("ftp通过文件名称获取远程文件流", e);
+        }
+        return is;
+    }
+
 }

+ 1 - 0
java110-utils/src/main/java/com/java110/utils/util/COSUtil.java

@@ -58,6 +58,7 @@ public class COSUtil {
         clientConfig.setHttpProtocol(HttpProtocol.https);
         // 3 生成 cos 客户端。
         COSClient cosClient = new COSClient(cred, clientConfig);
+
         return cosClient;
     }
 

+ 86 - 0
service-api/src/main/java/com/java110/api/controller/app/file/UserDownloadFileController.java

@@ -0,0 +1,86 @@
+package com.java110.api.controller.app.file;
+
+import com.java110.api.controller.app.payment.NotifyPaymentController;
+import com.java110.core.client.FileUploadTemplate;
+import com.java110.core.log.LoggerFactory;
+import com.java110.dto.userDownloadFile.UserDownloadFileDto;
+import com.java110.intf.job.IUserDownloadFileV1InnerServiceSMO;
+import com.java110.utils.util.Assert;
+import org.slf4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+
+/**
+ * 用户 下载中心中下载
+ */
+@RestController
+@RequestMapping(value = "/app/file/userfile")
+public class UserDownloadFileController {
+    private final static Logger logger = LoggerFactory.getLogger(NotifyPaymentController.class);
+
+    @Autowired
+    private IUserDownloadFileV1InnerServiceSMO userDownloadFileV1InnerServiceSMOImpl;
+
+    @Autowired
+    private FileUploadTemplate fileUploadTemplate;
+
+
+    @RequestMapping(path = "/download/{downloadId}", method = RequestMethod.GET)
+    public void download(@PathVariable String downloadId, HttpServletRequest request, HttpServletResponse response) {
+
+        logger.debug("用户开始下载文件" + downloadId);
+
+        String userId = request.getHeader("user-id");
+
+        UserDownloadFileDto userDownloadFileDto = new UserDownloadFileDto();
+        userDownloadFileDto.setDownloadId(downloadId);
+        userDownloadFileDto.setDownloadUserId(userId);
+        List<UserDownloadFileDto> userDownloadFileDtos = userDownloadFileV1InnerServiceSMOImpl.queryUserDownloadFiles(userDownloadFileDto);
+        Assert.listOnlyOne(userDownloadFileDtos, "文件不存在");
+
+        response.setHeader("content-type", "application/octet-stream");
+        response.setContentType("application/octet-stream");
+
+        InputStream is = null;
+        BufferedInputStream bis = null;
+        byte[] buffer = new byte[1024];
+
+        try {
+            is = fileUploadTemplate.downloadFile(userDownloadFileDtos.get(0).getTempUrl());
+            bis = new BufferedInputStream(is);
+            OutputStream os = response.getOutputStream();
+            int i = bis.read(buffer);
+            while (i != -1) {
+                os.write(buffer, 0, i);
+                i = bis.read(buffer);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+            if (bis != null) {
+                try {
+                    bis.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}