ylx 7 vuotta sitten
vanhempi
commit
062b245b2c

+ 33 - 33
ccb_fund_trusteeship/.classpath

@@ -1,33 +1,33 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" output="target/classes" path="src/main/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-			<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.5"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
-		<attributes>
-			<attribute name="owner.project.facets" value="java"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="output" path="target/classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+			<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.5"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="owner.project.facets" value="java"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 10 - 10
ccb_fund_trusteeship/.settings/org.eclipse.wst.common.component

@@ -1,10 +1,10 @@
-<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
-    <wb-module deploy-name="ccb_fund_trusteeship">
-        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
-        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
-        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
-        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
-        <property name="context-root" value="ccb_fund_trusteeship"/>
-        <property name="java-output-path" value="/ccb_fund_trusteeship/target/classes"/>
-    </wb-module>
-</project-modules>
+<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
+    <wb-module deploy-name="ccb_fund_trusteeship">
+        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
+        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
+        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
+        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
+        <property name="context-root" value="ccb_fund_trusteeship"/>
+        <property name="java-output-path" value="/ccb_fund_trusteeship/target/classes"/>
+    </wb-module>
+</project-modules>

+ 25 - 4
ccb_fund_trusteeship/src/main/java/com/fuzamei/utils/FileTransferUtil.java

@@ -2,6 +2,8 @@ package com.fuzamei.utils;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.springframework.web.multipart.MultipartFile;
 
@@ -14,7 +16,13 @@ public final class FileTransferUtil {
 		throw new AssertException("instaniation is not permitted");
 	}
 	
-	public static final void transfer(MultipartFile multipartFile,String directoryPath){
+	/**
+	 * 完成上传文件的功能
+	 * @param multipartFile	上传过来的文件对象
+	 * @param modifiedFilename	被修改后的文件名并且是要被添加到文件夹路径后面的名字
+	 * @param directoryPath	文件夹路径
+	 */
+	public static final void transfer(MultipartFile multipartFile, String modifiedFilename, String directoryPath){
 		File dir = new File(directoryPath);
 		if(!dir.isDirectory()){
 			throw new RuntimeException("路径需要是文件夹路径");
@@ -22,8 +30,7 @@ public final class FileTransferUtil {
 		if(!dir.exists()){
 			dir.mkdirs();
 		}
-		String filename = System.currentTimeMillis() + multipartFile.getOriginalFilename();//在文件名前面加一个时间戳
-		String filePath = directoryPath+SP+filename;
+		String filePath = directoryPath + modifiedFilename;
 		File newFile = new File(filePath);
 		try {
 			multipartFile.transferTo(newFile);
@@ -31,7 +38,21 @@ public final class FileTransferUtil {
 			if(newFile!=null){
 				newFile.delete();
 			}
-			throw new RuntimeException("文件上传出现异常");
+			throw new RuntimeException("文件上传出现异常:"+e.getMessage());
 		}
 	}
+	
+	/**
+	 * 
+	 * @return 返回false说明没有重复的文件名,返回true说明有重复的文件名
+	 */
+	public static final boolean checkIfHasSameFileName(MultipartFile...files){
+		int count = files.length;
+		Set<String> set = new HashSet<>();
+		for (MultipartFile file : files) {
+			set.add(file.getOriginalFilename());
+		}
+		int identity = set.size();
+		return identity==count?false:true;
+	}
 }

+ 8 - 0
ccb_fund_trusteeship/src/main/java/com/fuzamei/utils/RelativePathUtil.java

@@ -65,4 +65,12 @@ public class RelativePathUtil {
 		return prefix+formatPath()+sufix;
 	}
 	
+	/**
+	 * 给文件名前面加上时间戳
+	 * @param filename
+	 * @return
+	 */
+	public static final String timeStampAddFilename(String filename){
+		return System.currentTimeMillis()+filename;
+	}
 }

+ 8 - 15
ccb_fund_trusteeship/src/main/java/com/fuzamei/web/ProjectFileAction.java

@@ -33,14 +33,15 @@ import com.fuzamei.service.UserAuthoricationService;
 import com.fuzamei.utils.AbsolutePathUtil;
 import com.fuzamei.utils.AbsolutePathUtil;
 import com.fuzamei.utils.CalculateFileHashUtil;
+import com.fuzamei.utils.FileTransferUtil;
 import com.fuzamei.utils.HashXiZhiUtil;
 import com.fuzamei.utils.JSONUtil;
 import com.fuzamei.utils.PageDTO;
+import com.fuzamei.utils.RelativePathUtil;
 import com.fuzamei.utils.ValidationUtil;
 /**
  * 项目文档模块
  * @author ylx
- * 
  */
 @RestController
 @RequestMapping("/project_file")
@@ -68,7 +69,6 @@ public class ProjectFileAction {
 					"startTime":"",
 					"endTime":""
 				}
-	 * @param 
 	 * @param data 
 	 * @param @return
 	 * 设定文件 @return Map<String,Object> 返回类型
@@ -186,7 +186,6 @@ public class ProjectFileAction {
 	}
 	
 	/**
-	 * 
 	* @Title: showUploadProjectFileInterface
 	* @Description: TODO------------->>显示项目文档上传的界面
 	* 			以下为前端传递过来的参数
@@ -199,7 +198,6 @@ public class ProjectFileAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月15日 下午8:11:09
-	* @throws
 	 */
 	@RequestMapping(value = "/showUploadProjectFileInterface")
 	private Map<String, Object> showUploadProjectFileInterface(@RequestBody String data){
@@ -251,26 +249,21 @@ public class ProjectFileAction {
 				throw new RuntimeException(HintMSG.FILE_CANT_BE_NULL);
 			}
 			String path = AbsolutePathUtil.getAbsolutePath(request);//==================================================>>这个路径要改TODO
-			File dir=new File(path);
-			if(!dir.exists()){
-				dir.mkdirs();
-			}
+			String relativePath = "/ProjectFile/";
 			String filename = file.getOriginalFilename();//filename是上传文件的真实文件名
-			String pathFile=path+SP+filename;//pathFile是该文件在系统中的绝对路径
-			File newFile=new File(pathFile);
-			
-			String fileHash = CalculateFileHashUtil.calculateHash(file);//传入MultiPartFile直接计算hash值
-			
+			String modifiedFilename = RelativePathUtil.timeStampAddFilename(filename);//文件名加上前缀防止重名
+			String url=relativePath + modifiedFilename;//在文件名前面加一个时间戳,pathFile是该文件の绝对路径
+			String fileHash = CalculateFileHashUtil.calculateHash(file);//传入MultiPartFile直接计算hash值,并完成删除操作
 			Map<String, Object> mapToService =new LinkedHashMap<String, Object>();
 			mapToService.put("userDetail", userDetail);
 			mapToService.put("projectId", projectId);
 			mapToService.put("projectName", projectName);
-			mapToService.put("url", "/content/"+filename);
+			mapToService.put("url", url);
 			mapToService.put("attachmentName", filename);
 			mapToService.put("hash", fileHash);
 			projectFileService.insertInformationIntoTable(mapToService);
 			//文件最后上传,防止出现垃圾数据
-			file.transferTo(newFile);
+			FileTransferUtil.transfer(file, modifiedFilename ,path + relativePath);
 			return JSONUtil.getJsonMap(200, true, HintMSG.UPLOAD_SUCCESS,null);
 		} catch (Exception e) {
 			return JSONUtil.getJsonMap(500, false, HintMSG.UPLOAD_FAIL+":"+e.getMessage(),null);