Просмотр исходного кода

将B2B文件上传修改为上传到文件服务器上

git-svn-id: svn+ssh://10.10.101.21/source/platform/platform-b2b@7298 f3bf4e98-0cf0-11e4-a00c-a99a8b9d557d
suntg 9 лет назад
Родитель
Сommit
a18c7b8b96

+ 10 - 0
pom.xml

@@ -286,11 +286,21 @@
 			<groupId>com.github.sgroschupf</groupId>
 			<artifactId>zkclient</artifactId>
 		</dependency>
+		<!-- hessian -->
+		<dependency>
+			<groupId>com.caucho</groupId>
+			<artifactId>hessian</artifactId>
+		</dependency>
 		<!-- search on dubbo -->
 		<dependency>
 			<groupId>com.uas.search</groupId>
 			<artifactId>search-api-b2b</artifactId>
 		</dependency>
+		<!-- dfs on dubbo -->
+		<dependency>
+			<groupId>com.uas.dfs</groupId>
+			<artifactId>dfs-api</artifactId>
+		</dependency>
 	</dependencies>
 	<build>
 		<finalName>platform-b2b</finalName>

+ 31 - 11
src/main/java/com/uas/platform/b2b/controller/FileController.java

@@ -8,6 +8,7 @@ import java.io.OutputStream;
 
 import javax.servlet.http.HttpServletResponse;
 
+import com.uas.dfs.service.FileClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -29,6 +30,9 @@ public class FileController {
 	@Autowired
 	private AttachService attachService;
 
+	@Autowired
+	private FileClient fileClient;
+
 	/**
 	 * 文件下载
 	 * 
@@ -38,20 +42,36 @@ public class FileController {
 	 */
 	@RequestMapping("/{fileId}")
 	public void download(@PathVariable("fileId") long fileId, HttpServletResponse response) throws IOException {
-		Attach attch = attachService.getAttach(fileId);
-		File file = new File(attch.getPath());
-		InputStream in = new FileInputStream(file);
-		OutputStream os = response.getOutputStream();
-		response.addHeader("Content-Disposition", "attachment;filename=" + new String(attch.getName().getBytes("utf-8"), "iso-8859-1"));
-		response.addHeader("Content-Length", String.valueOf(file.length()));
+		Attach attach = attachService.getAttach(fileId);
+		if(attach == null) throw  new IllegalArgumentException("附件不存在");
+		response.addHeader("Content-Disposition", "attachment;filename=" + new String(attach.getName().getBytes("utf-8"), "iso-8859-1"));
 		response.setCharacterEncoding("utf-8");
 		response.setContentType("application/octec-stream");
-		int data = 0;
-		while ((data = in.read()) != -1) {
-			os.write(data);
+		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://")) {
+			// 存放在其他网络资源中,直接跳转至链接地址
+			response.sendRedirect(path);
+		} else {
+			// 其他的当做是存放在本地服务器上,去本地服务器根据文件路径去获取
+			File file = new File(attach.getPath());
+			if(!file.exists()) throw new IllegalArgumentException("附件不存在");
+			response.addHeader("Content-Length", String.valueOf(file.length()));
+			InputStream in = new FileInputStream(file);
+			OutputStream os = response.getOutputStream();
+			int data = 0;
+			while ((data = in.read()) != -1) {
+				os.write(data);
+			}
+			in.close();
+			os.close();
 		}
-		in.close();
-		os.close();
 	}
 
 }

+ 0 - 8
src/main/java/com/uas/platform/b2b/service/AttachService.java

@@ -69,12 +69,4 @@ public interface AttachService {
 	 */
 	public Map<String, String> uploadZip(FileUpload uploadItem, String parentDir, String description);
 
-	/**
-	 * 产生文件唯一名字
-	 * 
-	 * @param fileName
-	 * @param parentDir
-	 * @return
-	 */
-	public String randomFileName(String fileName, String parentDir);
 }

+ 5 - 2
src/main/resources/spring/dubbo-consumer.xml

@@ -4,13 +4,16 @@
 	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
-	<dubbo:application name="search_b2b_consumer" owner="${dubbo.owner}" />
+	<dubbo:application name="b2b_consumer" />
 
 	<dubbo:registry address="${zk.url}" check="false" />
 
 	<!-- B2B搜索服务 -->
 	<dubbo:reference id="searchService"
 		interface="com.uas.search.b2b.service.SearchService" timeout="10000"
-		group="${dubbo.group}" />
+		group="${dubbo.group}" owner="${dubbo.owner}" />
+
+	<!-- 分布式文件服务 -->
+	<dubbo:reference id="fileClient" interface="com.uas.dfs.service.FileClient" timeout="100000"/>
 
 </beans>