webupload大文件分片上传
1. 大文件分片上传的js代码
2.大文件上传的java后台代码
public String fileUpload(@RequestParam("file") MultipartFile file, String chunks, String chunk,
String name, HttpServletRequest request, HttpServletResponse response, String fileType) throws IOException {
log.info("FileManageController.fileUpload request={}", request);
InputStream proIn = getClass().getClassLoader().getResourceAsStream("uploadConfig.properties");
Properties properties = new Properties();
properties.load(proIn);
String fileSystemPath = properties.getProperty("fileManagerSystemPath");
String tempFileSystemPath = properties.getProperty("uploadFileSystemPath");
String fileName = file.getOriginalFilename();
String savePath = fileSystemPath + "/" + fileType;
JSONObject json = new JSONObject();
File retfile = new File(savePath, fileName);
if (!retfile.exists()) {
retfile.mkdirs();
}
json.put("filePath", retfile.getPath());
try {
if (null != file) {
if (null == chunks && null == chunk) {
File targetFile = new File(savePath, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
file.transferTo(targetFile);
targetFile.createNewFile();
json.put("msg", "success");
return json.toJSONString();
}
String tempFileDir = tempFileSystemPath + File.separator + name;
File parentFileDir = new File(tempFileDir);
if (!parentFileDir.exists()) {
parentFileDir.mkdirs();
}
File f = new File(tempFileDir + File.separator + name + "_" + chunk + ".part");
file.transferTo(f);
f.createNewFile();
boolean uploadDone = true;
for (int i = 0; i < Integer.parseInt(chunks); i++) {
File partFile = new File(tempFileDir, name + "_" + i + ".part");
if (!partFile.exists()) {
uploadDone = false;
json.put("msg", "success");
return json.toJSONString();
}
}
if (uploadDone) {
synchronized (this) {
File destTempFile = new File(savePath, name);
for (int i = 0; i < Integer.parseInt(chunks); i++) {
File partFile = new File(tempFileDir, name + "_" + i + ".part");
if (retfile.exists() && i == 0) {
retfile.delete();
retfile.createNewFile();
}
FileOutputStream destTempfos = new FileOutputStream(destTempFile, true);
FileUtils.copyFile(partFile, destTempfos);
destTempfos.close();
}
FileUtils.deleteDirectory(parentFileDir);
}
}
json.put("msg", "success");
log.info("FileManageController.fileUpload response={}", json.toJSONString());
return json.toJSONString();
}
} catch (Exception e) {
e.printStackTrace();
log.error("FileManageController.fileUpload error={}", e.getMessage());
}
json.put("msg", "success");
return json.toJSONString();
}