/**
* 图片文件上传
*/
@ResponseBody
@RequestMapping(value = "/imgUpload.do", method = RequestMethod.POST)
public ServerResponse<String> photoUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException {
String path = null;
String type = null;
if (file != null) {
String fileName = file.getOriginalFilename();
System.out.println("上传的文件原名称:" + fileName);
type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
if (type != null) {
if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
String realPath = request.getSession().getServletContext().getRealPath("/");
String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
path = realPath +trueFileName;
System.out.println("存放图片文件的路径:" + path);
File resultFile = new File(path)
if (!resultFile .getParentFile().exists()) {
resultFile .getParentFile().mkdirs();
}
file.transferTo(resultFile);
System.out.println("文件成功上传到指定目录下");
} else {
System.out.println("不是我们想要的文件类型,请按要求重新上传");
return ServerResponse.createByErrorMessage("文件类型不正确");
}
} else {
System.out.println("文件类型为空");
return ServerResponse.createByErrorMessage("文件类型为空");
}
} else {
System.out.println("没有找到相对应的文件");
return ServerResponse.createByErrorMessage("没有找到相对应的文件");
}
return ServerResponse.createBySuccessMessage(path);
}