问题在于:
@ResponseBody // 这里去掉
@RequestMapping(params = "method=uploadFile")
public void uploadFile(@RequestParam("file") List<MultipartFile> file,
HttpServletRequest request,
HttpServletResponse response // 添加
) {
response.setContentType("text/html;chartset=UTF-8"); // 添加
response.setCharacterEncoding("UTF-8"); // 添加
//把文件放在部署路径下的用户ID文件夹中
String webPath = request.getSession().getServletContext().getRealPath("/");
String filePath = "uploaddir/";
File dir = new File(webPath+filePath);
if (!dir.exists()) {
dir.mkdirs();
}
List<Map<String,Object>> resultList = new ArrayList<>();
Map<String, Object> result = null;
Map<String, Object> resultmap = new HashMap<>();
try {
for(MultipartFile myfile : file){
String filename = getFilename(myfile.getOriginalFilename());
String newFileName = System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
String uuid= AppUtil.getUUID();
//上传文件
File tfile = new File(webPath+filePath, newFileName);
myfile.transferTo(tfile);
//将文件名和文件路径返回
result = new HashMap<String, Object>();
result.put("ID", uuid);// 文件添加一个ID
result.put("filename", filename);//原文件名
result.put("newFileName", newFileName);//新文件名
result.put("filepath", filePath + newFileName);//文件全路径
result.put("fullfilepath", webPath+filePath + newFileName);//文件全路径
resultList.add(result);
}
resultmap.put("success", "1");
resultmap.put("list", resultList);
response.getWriter().write(JSON.toJSONString(resultList)); // 添加
} catch (IOException e) {
resultmap.put("success", "0");
logger.error(e.getMessage());
}
String str = JSON.toJSONString(resultmap, SerializerFeature.DisableCircularReferenceDetect);
logger.debug("结果:{}", str);// debug 输出结果
// return str; // 不能有返回值
}
ajaxfileupload 不需要改什么东西