在Controller中以一个类作为返回结果,然后解析成JSON传输给JSP页面。
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public AjaxResult upload(HttpServletRequest request, HttpServletResponse response) {
AjaxResult result = new AjaxResult();
String refSys = request.getParameter("refSys");
String foreignId = request.getParameter("foreignId");
String version = request.getParameter("version");
String remark = request.getParameter("remark");
String keyword = request.getParameter("keywords");
String callback = request.getParameter("callback");
String type = request.getParameter("type");
LogUtils.debug("文件上传", refSys, foreignId, version, keyword);
try {
Map<String, Object> map = new HashMap<String, Object>();
if (!StringUtils.isEmpty(refSys)) {
refSys = URLDecoder.decode(refSys, "UTF-8").toUpperCase();
}
if (!StringUtils.isEmpty(foreignId)) {
foreignId = URLDecoder.decode(foreignId, "UTF-8").toUpperCase();
}
if (!StringUtils.isEmpty(version)) {
version = URLDecoder.decode(version, "UTF-8").toUpperCase();
}
if (!StringUtils.isEmpty(remark)) {
remark = URLDecoder.decode(remark, "UTF-8");
}
String[] keywords = null;
if (!StringUtils.isEmpty(keyword)) {
keyword = URLDecoder.decode(keyword, "UTF-8").toUpperCase();
keywords = keyword.split("[,;\\s\\t\\n]+");
}
Collection<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("files");
if (files == null || files.isEmpty()) {
files = new ArrayList<MultipartFile>();
MultiValueMap<String, MultipartFile> mfiles= ((MultipartHttpServletRequest)request).getMultiFileMap();
Map<String, MultipartFile> singleFileMap = mfiles.toSingleValueMap();
Set<Map.Entry<String, MultipartFile>> fileSet = singleFileMap.entrySet();
for(Map.Entry<String, MultipartFile> file : fileSet) {
MultipartFile myFile = file.getValue();
files.add(myFile);
}
/* Map<String, MultipartFile> mfiles = ((MultipartHttpServletRequest) request).getFileMap();
for (Entry<String, MultipartFile> file : mfiles.entrySet()) {
MultipartFile myfile = file.getValue();// 获取上传文件对象
files.add(myfile);
}*/
}
if (files != null && !files.isEmpty()) {
BaseFile file = null;
if ("mongo".equals(filePersistType)) {
file = new MongoDbFilePo();
} else {
file = new FileInfoPo();
}
file.setRefSys(refSys);
file.setForeignId(foreignId);
// 存储文件
file = (BaseFile) getFileService().uploadFile(files, file, version, remark, keywords);
map.put("id", file.getId());
map.put("foreignId", file.getForeignId());
map.put("version", file.getVersion());
if (!StringUtils.isEmpty(file.getPassword())) {
map.put("pwd", file.getPassword());
}
result.setData(map);
result.setMessage("文件上传成功!");
} else {
result.setSuccess(false);
LogUtils.debug("文件上传-未选择文件", refSys, foreignId, version, keyword);
result.setMessage("未选择上传的文件!");
}
} catch (Exception e) {
LogUtils.debug("文件上传-失败");
result.setSuccess(false);
result.setMessage("文件上传失败,请联系管理员!");
LogUtils.error("文件保存失败", e, refSys, foreignId, version, keyword);
}
if ("form".equalsIgnoreCase(type)) {
request.setAttribute("result", JSONObject.fromObject(result).toString());
request.setAttribute("callback", callback);
try {
request.getRequestDispatcher("/WEB-INF/jsp/file/form_result.jsp").forward(request, response);
return null;
} catch (ServletException e) {
LogUtils.error("", e);
} catch (IOException e) {
LogUtils.error("", e);
}
}
return result;
}
AjaxResult为返回结果类:
public class AjaxResult {
private boolean success = true;
private String message ;
Map<String, Object> data;
public AjaxResult(boolean success){
this.success = success;
}
public AjaxResult(boolean success,String message){
this.success = success;
this.message = message;
}
public AjaxResult(){
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}
}
所解析成的JSON语句为:"{"success":true,"message":"文件上传成功!","data":{"foreignId":"12345678","id":"8a8c8cb85184b866015185576ca60000","version":null}}"
外层,success,message,data为AjaxResult类中的属性,(如果这些类属性的set/get方法不是按照默认规则写的,实际结成JSON语句时,外层显示的名称为实际使用的set/get方法去掉"set","get"字符前缀后的名称。)