소스 검색

更新文件上传下载为HTTP请求方式

hejq 7 년 전
부모
커밋
197249fe1a

+ 40 - 18
src/main/java/com/uas/platform/b2b/controller/FileController.java

@@ -1,6 +1,5 @@
 package com.uas.platform.b2b.controller;
 
-import com.uas.dfs.service.FileClient;
 import com.uas.platform.b2b.dao.CommonDao;
 import com.uas.platform.b2b.model.Attach;
 import com.uas.platform.b2b.model.PurchaseOrderAll;
@@ -31,15 +30,42 @@ public class FileController {
 	@Autowired
 	private AttachService attachService;
 
-	@Autowired
-	private FileClient fileClient;
-
 	@Autowired
     private PurchaseOrderService purchaseOrderService;
 
 	@Autowired
     private CommonDao commonDao;
 
+    /**
+     * DFS文件服务器地址
+     */
+	private static final String DFS_URL = "http://dfs.ubtob.com";
+
+    /**
+     * HTTP地址头部
+     */
+	private static final String HTTP_HEAD = "http://";
+
+    /**
+     * HTTPS地址头部
+     */
+    private static final String HTTPS_HEAD = "https://";
+
+    /**
+     * FTP地址头部
+     */
+    private static final String FTP_HEAD = "ftp://";
+
+    /**
+     * SFTP地址头部
+     */
+    private static final String SFTP_HEAD = "sftp://";
+
+    /**
+     * ERP文件下载
+     */
+    private static final String FILE_DOWNLOAD_ACTION = "downloadbyId.action";
+
 	/**
 	 * 文件下载
 	 * 
@@ -60,22 +86,15 @@ public class FileController {
 		response.setCharacterEncoding("utf-8");
 		response.setContentType("application/octec-stream");
 		String path = attach.getPath();
-		if(path.startsWith("http://dfs.ubtob.com")) {// 存储在dfs存储服务器,去dfs存储服务器下载
-			byte[] fileBytes = fileClient.download(attach.getPath());
-			if (fileBytes.length <= 0) {
-			    throw new IllegalArgumentException("附件不存在");
-            }
-			response.addHeader("Content-Length", String.valueOf(fileBytes.length));
-			OutputStream os = response.getOutputStream();
-			os.write(fileBytes);
-			os.close();
-		} else if (path.startsWith("http://") || path.startsWith("https://") || path.startsWith("ftp://") || path.startsWith("sftp://")) {
+        if (path.startsWith(HTTP_HEAD) || path.startsWith(HTTPS_HEAD) || path.startsWith(FTP_HEAD) || path.startsWith(SFTP_HEAD)) {
 			// 存放在其他网络资源中,直接跳转至链接地址
 			response.sendRedirect(path);
 		} else {
 			// 其他的当做是存放在本地服务器上,去本地服务器根据文件路径去获取
 			File file = new File(attach.getPath());
-			if(!file.exists()) throw new IllegalArgumentException("附件不存在");
+			if (!file.exists()) {
+			    throw new IllegalArgumentException("附件不存在");
+            }
             InputStream in = new FileInputStream(path);
             OutputStream os = response.getOutputStream();
             response.addHeader("Content-Disposition", "attachment;filename=" + new String(attach.getName().getBytes("utf-8"), "iso-8859-1"));
@@ -109,10 +128,13 @@ public class FileController {
         String url = null;
         if (!CollectionUtils.isEmpty(attaches)) {
            for (Attach attach : attaches) {
-               if (attach.getPath().startsWith("http://dfs.ubtob.com")) {// 存储在dfs存储服务器,去dfs存储服务器下载
+               boolean httpFile = (attach.getPath().startsWith(HTTP_HEAD) || attach.getPath().startsWith(HTTPS_HEAD) || attach.getPath().startsWith(FTP_HEAD)
+                       || attach.getPath().startsWith(SFTP_HEAD))
+                       && !attach.getPath().contains(FILE_DOWNLOAD_ACTION);
+               // 存储在dfs存储服务器,去dfs存储服务器下载
+               if (attach.getPath().startsWith(DFS_URL)) {
                 // TODO dfs服务器上打包下载
-               } else if ((attach.getPath().startsWith("http://") || attach.getPath().startsWith("https://") || attach.getPath().startsWith("ftp://") || attach.getPath().startsWith("sftp://"))
-                       && !attach.getPath().contains("downloadbyId.action")) {
+               } else if (httpFile) {
                    // 存放在其他网络资源中,直接跳转至链接地址
                    response.sendRedirect(attach.getPath());
                } else {

+ 21 - 14
src/main/java/com/uas/platform/b2b/service/impl/AttachServiceImpl.java

@@ -1,7 +1,5 @@
 package com.uas.platform.b2b.service.impl;
 
-import com.alibaba.fastjson.JSONObject;
-import com.uas.dfs.service.FileClient;
 import com.uas.platform.b2b.core.util.PathUtils;
 import com.uas.platform.b2b.dao.AttachDao;
 import com.uas.platform.b2b.model.Attach;
@@ -11,7 +9,6 @@ import com.uas.platform.b2b.support.HttpUtils;
 import com.uas.platform.b2b.support.SystemSession;
 import com.uas.platform.b2b.temporary.model.FileUrl;
 import com.uas.platform.core.util.FileUtils;
-import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
@@ -20,14 +17,21 @@ import java.io.File;
 import java.io.IOException;
 import java.util.*;
 
+/**
+ * 文件服务
+ *
+ * @author hejq
+ */
 @Service
 public class AttachServiceImpl implements AttachService {
 
 	@Autowired
 	private AttachDao attachDao;
 
-	@Autowired
-	private FileClient fileClient;
+    /**
+     * 文件最大100M
+     */
+	private final Integer MAX_FILE_SIZE = 100 * 1024 * 1024;
 
 	@Override
 	public Attach getAttach(Long id) {
@@ -52,7 +56,7 @@ public class AttachServiceImpl implements AttachService {
 	public Attach upload(FileUpload uploadItem, String description) {
 		String filename = uploadItem.getFile().getOriginalFilename();
 		long size = uploadItem.getFile().getSize();
-		if(size >  100 * 1024 * 1024) {// 100Mb
+		if(size >  MAX_FILE_SIZE) {
 			// 是否限制文件上传大小
 		}
 		String path = null;
@@ -61,7 +65,6 @@ public class AttachServiceImpl implements AttachService {
         } catch (Exception e) {
             e.printStackTrace();
         }
-//		String path = fileClient.upload(uploadItem.getFile().getBytes(), size, FilenameUtils.getExtension(filename), null);
 		Attach attach = new Attach(filename, path, description, size, new Date());
 		return attach;
 	}
@@ -78,7 +81,8 @@ public class AttachServiceImpl implements AttachService {
 					File file = new File(fileMap.get(entry));
 					if (file.exists()) {
 						Map<String, Object> params = entryParams.get(entry);
-						sources = params.get("sourceId").toString().split(",");// multi sources
+                        // multi sources
+						sources = params.get("sourceId").toString().split(",");
 						for (String sr : sources) {
 							Attach attach = new Attach(String.valueOf(params.get("name")), fileMap.get(entry), description, file.length(),
 									new Date());
@@ -125,17 +129,20 @@ public class AttachServiceImpl implements AttachService {
 	private String getDir(String parentDir) {
 		String path = PathUtils.getFilePath() + "postattach";
 		File dir = new File(path);
-		if (!dir.isDirectory())
-			dir.mkdir();
+		if (!dir.isDirectory()) {
+            dir.mkdir();
+        }
 		path += File.separator + parentDir;
 		dir = new File(path);
-		if (!dir.isDirectory())
-			dir.mkdir();
+		if (!dir.isDirectory()) {
+            dir.mkdir();
+        }
 		if (SystemSession.getUser() != null) {
 			path += File.separator + SystemSession.getUser().getEnterprise().getUu();
 			dir = new File(path);
-			if (!dir.isDirectory())
-				dir.mkdir();
+			if (!dir.isDirectory()) {
+                dir.mkdir();
+            }
 		}
 		return path;
 	}

+ 1 - 0
src/main/java/com/uas/platform/b2b/support/FileUploadHttp.java

@@ -31,6 +31,7 @@ import java.util.*;
  * Created by hejq on 2017-10-10.
  */
 public class FileUploadHttp {
+
     /**
      * 发起POST、PUT请求
      *

+ 0 - 3
src/main/resources/spring/dubbo-consumer.xml

@@ -13,9 +13,6 @@
 		interface="com.uas.search.b2b.service.SearchService" timeout="10000"
 		group="${dubbo.group}" owner="${dubbo.owner}" />
 
-	<!-- 分布式文件服务 -->
-	<dubbo:reference id="fileClient" interface="com.uas.dfs.service.FileClient" timeout="100000"/>
-	
 	<!-- 邮件服务 -->
 	<dubbo:reference id="mailService"
 		interface="com.uas.message.mail.service.MailService" timeout="10000" />