springmvc利用jqueryupload上传文件,后台处理方法
public void importIdentifySchemeFile(Integer id,Integer type,HttpServletRequest request, HttpServletResponse response){
PrintWriter out = null;
try {
//初始化变量
InputStream stream = null;
String ret_fileName = null;//返回给前端已修改的文件名称
Map<String, Object> result = new HashMap<String, Object>();
SkillIdentifyFile skillIdentifyFile = new SkillIdentifyFile();
String fileSrc;
String fileName;
File uploadedFile = null;
try {
//设置字符集
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
out = response.getWriter();
if(id==null){
result.put("message", "方案不存在,不能上传");
out.print(new JSONObject(result).toString());
return;
}
//获取文件流
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
MultipartFile multipartFile = null;
for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
multipartFile = set.getValue();//文件名
}
stream = multipartFile.getInputStream();
//初始化文件名,并定义上传文件
UUID uuid = UUID.randomUUID();
fileName = multipartFile.getOriginalFilename();//原始文件名
String suffix = fileName.substring(fileName.indexOf(".") + 1);
ret_fileName = uuid.toString() + "." + suffix;
// 文件保存目录路径
String savePath = FileSystemConfig.getFileRootPath() + FILE_PATH;
// 创建文件夹
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
fileSrc = savePath + File.separator + ret_fileName;
uploadedFile = new File(fileSrc);
FileUtils.saveFile(uploadedFile, stream);
} catch (Exception e) {
//读写错误异常处理
result.put("message", e.getMessage());
out.print(new JSONObject(result).toString());
return;
} finally {
//资源关闭异常处理
try {
if (stream != null) {
stream.close();
}
} catch (Exception e) {
result.put("message", e.getMessage());
out.print(new JSONObject(result).toString());
return;
}
}
//初始化方案附件属性
skillIdentifyFile.setFile(FILE_PATH + ret_fileName);//下载路径
skillIdentifyFile.setName(fileName);//原始文件名
skillIdentifyFile.setUpdateTime(new Date(System.currentTimeMillis()));
skillIdentifyFile.setSkillidentifyId(id);
skillIdentifyFile.setType(type);
//保存当前附件到数据库
skillIdentifyService.addSkillIdentifyFile(skillIdentifyFile);
result.put("message", File_SUCCESS);
result.put("fileName", fileName);
out.print(new JSONObject(result).toString());
} finally {
//将信息返回
if (out != null) {
out.flush();
out.close();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。