//测试上传FSN文件
@RequestMapping("FSNUpload")
@ResponseBody
public JSONObject imgUpload(HttpServletRequest request) throws IllegalStateException, IOException {
JSONObject json = TestController.uploadImg(request, PropertiesUtil.getProperty("upload.path"));
return json;
}
public static JSONObject uploadImg(HttpServletRequest request, String imgParentDir) {
JSONObject json = new JSONObject();
// 将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 检查form中是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
// 将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 获取multiRequest 中所有的文件名
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 一次遍历所有文件
MultipartFile file = multiRequest.getFile(iter.next().toString());
if (file != null) {
String serverPath = request.getSession().getServletContext().getRealPath(imgParentDir);
// imgParentDir(/upload/good/)
String filePath = new Date().getTime() + file.getOriginalFilename();
String path = serverPath + "/" + filePath;
// 上传
try {
file.transferTo(new File(path));
json.put("imgPath", imgParentDir + filePath);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return json;
}
控制器中上传代码如上