1.简单的jsp页面:index.jsp
//代码段
<form:form method="post" action="../user/saveScheduleFile" enctype="multipart/form-data">
<label>选择一个文件上传</label>
<input type="file" name="file"/>
<input type="submit" value="submit" class="btn btn-primary"/>
</form:form>
2.controller页面
@RequestMapping(value = "/saveScheduleFile", method = RequestMethod.POST)
@ResponseBody
public ResponseParam saveScheduleFile(@RequestParam(value = "file", required = false) MultipartFile file,Model model) throws IOException {
List<SchProjectTask> result = new ArrayList<>();
ProjectReader reader = new MPPReader();
ResponseParam response = new ResponseParam();
try {
ProjectFile project = reader.read(file.getInputStream());
result = MppUtil.getTaskList(project);
response.setCode("0");
response.setData(result);
} catch (MPXJException e) {
e.printStackTrace();
}
return response;
}
3.封装json类:
public class ResponseParam {
private String code; // 错误代码
private String encrypt; // 传入参数是否加密 Y 加密;N 不加密
private String message; // 出错信息描述,不加密
private Object data; // 返回数据,如果加密,base64字符串;不加密,json字符串
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getEncrypt() {
return encrypt;
}
public void setEncrypt(String encrypt) {
this.encrypt = encrypt;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}