Sfoglia il codice sorgente

修正win使用文件上传的问题

huangtf 2 anni fa
parent
commit
3d5f36509f

+ 25 - 4
blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/LargeFileEndpoint.java

@@ -311,7 +311,14 @@ public class LargeFileEndpoint {
 		String filePath =ParamCache.getValue(CommonConstant.SYS_LOCAL_URL)+"largeFile/";
 
 		File tempFile = buildUploadFile(tempFileName);
-		param.getFile().transferTo(tempFile);
+
+		//为了防止win路径有时会抛异常,增加一个处理
+		try {
+			param.getFile().transferTo(tempFile);
+		} catch (Exception e) {
+			writeMultipartFileToFile(param,tempFile);
+		}
+
 
 		/**
 		 * 以上意思是把每个分片都保存成本地一个文件,如 测试.mp4.1 最后合并
@@ -360,9 +367,13 @@ public class LargeFileEndpoint {
 				NewBladeFile newBladeFile = new NewBladeFile();
 				if(param.getFilename().contains("pdf")){
 //					FileInputStream inputStream1 = new FileInputStream(filePath + param.getFilename());
-					PDDocument document = PDDocument.load(multipartFile.getInputStream());
-					//获取文件页数
-					newBladeFile.setPage(document.getPages().getCount());
+					try {
+						PDDocument document = PDDocument.load(multipartFile.getInputStream());
+						//获取文件页数
+						newBladeFile.setPage(document.getPages().getCount());
+					} catch (IOException e) {
+						e.printStackTrace();
+					}
 					//pdf的路径就是文件上传的路径
 					newBladeFile.setPdfUrl(bladeFile.getLink());
 				}else if(param.getFilename().contains("xlsx") || param.getFilename().contains("xls")){
@@ -398,6 +409,16 @@ public class LargeFileEndpoint {
 		return result;
 	}
 
+	public void writeMultipartFileToFile(MultipartFileParam param, File tempFile) throws IOException {
+		byte[] data = param.getFile().getBytes(); // 获取二进制数据
+		FileOutputStream outputStream = new FileOutputStream(tempFile);
+		try {
+			outputStream.write(data); // 将二进制数据写入到文件中
+		} finally {
+			outputStream.close(); // 关闭输出流
+		}
+	}
+
 	/**
 	 *file 转 MultipartFile
 	 * **/