Commit 00e5666c authored by neo.wang's avatar neo.wang

Merge branch 'dev_neo2' into 'dev'

Dev neo2

See merge request root/atms!25
parents 7093cc1d f7d943cd
...@@ -12,6 +12,11 @@ import org.springframework.web.multipart.MultipartHttpServletRequest; ...@@ -12,6 +12,11 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
import pwc.taxtech.atms.constant.enums.EnumModule; import pwc.taxtech.atms.constant.enums.EnumModule;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
...@@ -48,12 +53,12 @@ public class FileUploadAdapter { ...@@ -48,12 +53,12 @@ public class FileUploadAdapter {
public ResponseEntity upload(MultipartHttpServletRequest request, EnumModule trailBalance, UUID userIdForUpload) { public ResponseEntity upload(MultipartHttpServletRequest request, EnumModule trailBalance, UUID userIdForUpload) {
if (request.getFileMap().size() <= 0) return ResponseEntity.badRequest().body("NoFile"); if (request.getFileMap().size() <= 0) return ResponseEntity.badRequest().body("NoFile");
FileParamBean paramBean = getQueryStringParameters(request); FileParamBean paramBean = getQueryStringParameters(request);
if (!paramBean.result) return ResponseEntity.badRequest().body("ParametersInvalid"); if (!paramBean.result) return ResponseEntity.badRequest().body("ParametersInvalid");
if (paramBean.totalSize > Long.valueOf(maxFileSize)) return ResponseEntity.badRequest().body("InvalidFileSize"); if (paramBean.totalSize > Long.valueOf(maxFileSize))
return ResponseEntity.badRequest().body("InvalidFileSize");
String baseFolder = prepareFolder(); String baseFolder = prepareFolder();
if (!Utils.isEmpty(baseFolder)) return ResponseEntity.badRequest().body("PrepareFolderError"); if (Utils.isEmpty(baseFolder)) return ResponseEntity.badRequest().body("PrepareFolderError");
String tempPath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName); String tempPath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName);
String completePath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName); String completePath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName);
...@@ -68,23 +73,38 @@ public class FileUploadAdapter { ...@@ -68,23 +73,38 @@ public class FileUploadAdapter {
if (completeFile.exists()) completeFile.delete(); if (completeFile.exists()) completeFile.delete();
} }
if(paramBean.isLastChunk){ try (FileOutputStream fos = new FileOutputStream(new File(tempPath));
InputStream inputStream = request.getFileMap().get("file").getInputStream();) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) > 0) {
fos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (paramBean.isLastChunk) {
LOGGER.debug("Last chunk arrived"); LOGGER.debug("Last chunk arrived");
File completeFile = new File(completePath); File completeFile = new File(completePath);
File tempFile = new File(tempPath); File tempFile = new File(tempPath);
tempFile.renameTo(completeFile); tempFile.renameTo(completeFile);
LOGGER.info("{} upload to temp folder sucess",paramBean.fileName); LOGGER.info("{} upload to temp folder sucess", paramBean.fileName);
} }
}else { } else {
File tempFile = new File(tempPath); File tempFile = new File(tempPath);
File completeFile = new File(completePath); File completeFile = new File(completePath);
if (tempFile.exists()) tempFile.delete(); if (tempFile.exists()) tempFile.delete();
if (completeFile.exists()) completeFile.delete(); if (completeFile.exists()) completeFile.delete();
} }
return ResponseEntity.created(URI.create("")).body(paramBean.tempFileName);
return null;
} }
private FileParamBean getQueryStringParameters(MultipartHttpServletRequest request) { private FileParamBean getQueryStringParameters(MultipartHttpServletRequest request) {
...@@ -107,7 +127,7 @@ public class FileUploadAdapter { ...@@ -107,7 +127,7 @@ public class FileUploadAdapter {
} }
private String prepareFolder() { private String prepareFolder() {
String uploadFolder = String.format("~/%s", UPLOAD_FOLDER_NAME); String uploadFolder = String.format("~"+File.separator+"%s", UPLOAD_FOLDER_NAME);
if (containsSubFolder) { if (containsSubFolder) {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
now.setTime(new Date()); now.setTime(new Date());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment