/**
*
* 上传通用
*
* */
@RequestMapping(value = "/reportUploader", method = RequestMethod.POST)
public void uploadMajInnovation(HttpServletRequest request, HttpServletResponse response, @RequestParam("access_token") String access_token) {
Teacher teacher = (Teacher) request.getServletContext().getAttribute(access_token);
teacher.getSchoolNumber();
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 得到所有的表单域,它们目前都被当作FileItem
List<FileItem> fileItems = upload.parseRequest(request);
String id = "";
String fileName = "";
// 如果大于1说明是分片处理
int chunks = 1;
int chunk = 0;
FileItem tempFileItem = null;
for (FileItem fileItem : fileItems) {
if (fileItem.getFieldName().equals("id")) {
id = fileItem.getString();
} else if (fileItem.getFieldName().equals("name")) {
fileName = new String(fileItem.getString().getBytes("ISO-8859-1"), "UTF-8");
} else if (fileItem.getFieldName().equals("chunks")) {
chunks = NumberUtils.toInt(fileItem.getString());
} else if (fileItem.getFieldName().equals("chunk")) {
chunk = NumberUtils.toInt(fileItem.getString());
} else if (fileItem.getFieldName().equals("multiFile")) {
tempFileItem = fileItem;
}
}
String filePath = ConfigUtil.static_dir + teacher.getSchoolNumber() + "/development/";
String realname = fileName.substring(0, fileName.lastIndexOf("."));
// 临时目录用来存放所有分片文件
String tempFileDir = filePath + File.separator + id;
File parentFileDir = new File(tempFileDir);
if (!parentFileDir.exists()) {
parentFileDir.mkdirs();
}
// 分片处理时,前台会多次调用上传接口,每次都会上传文件的一部分到后台
File tempPartFile = new File(parentFileDir, realname + ".part");
FileUtils.copyInputStreamToFile(tempFileItem.getInputStream(), tempPartFile);
// 是否全部上传完成
// 所有分片都存在才说明整个文件上传完成
boolean uploadDone = true;
for (int i = 0; i < chunks; i++) {
File partFile = new File(parentFileDir, realname+ ".part");
if (!partFile.exists()) {
uploadDone = false;
}
}
// 所有分片文件都上传完成
// 将所有分片文件合并到一个文件中
if (uploadDone) {
File destfile = new File(filePath);
if (!destfile.exists()) {
destfile.mkdirs();
}
// 得到 destTempFile 就是最终的文件
File destTempFile = new File(filePath, realname + "."+ fileName.substring(fileName.lastIndexOf(".") + 1));
File partFile = new File(parentFileDir, realname + ".part");
if(!destTempFile.exists()){
for (int i = 0; i < chunks; i++) {
FileOutputStream destTempfos = new FileOutputStream(destTempFile, true);
// 遍历"所有分片文件"到"最终文件"中
FileUtils.copyFile(partFile, destTempfos);
destTempfos.close();
}
}
//文件重命名(时间戳)
Long newname = new Date().getTime();
destTempFile.renameTo(new File(filePath, newname + "."+ fileName.substring(fileName.lastIndexOf(".") + 1)));
request.getSession().removeAttribute("EnterpriseFile");
// 将文件信息存储在session中
CareerPlanFile cpf = new CareerPlanFile();
cpf.setFileName(fileName);
cpf.setFilePath(destTempFile.getAbsolutePath());
request.getSession().setAttribute("EnterpriseFile", cpf);
// 删除临时目录中的分片文件
FileUtils.deleteDirectory(parentFileDir);
} else {
// 临时文件创建失败
if (chunk == chunks - 1) {
FileUtils.deleteDirectory(parentFileDir);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
网友评论