您的位置:首页 > 编程语言 > Java开发

SpringMvc 上传excel(注解和非注解两种方式)

2017-03-22 09:28 197 查看

本文转自:http://blog.csdn.net/forever_insist/article/details/51146024

1、第一种方式:

A:JSP页面:

<form name="importForm" action="${ctx }/service/userService/BatchImport.do" method="post" enctype="multipart/form-data">

请选择上传所需文件:<input type="file" id="theFile" name="theFile" /><br/>
<input  type="submit" value="提交"/>
</form>
B: Controller 层
@Controller
@RequestMapping("/service/userService")
public class LoadUserServiceAction  {

@Reference
UserHelper userHelper;
@Reference
ClassHelper classHelper;
@Reference
InstitutionHelper institutionHelper;

@RequestMapping("/BatchImport")
public void BatchImport(HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter pw = null;
request.setCharacterEncoding("utf-8");
int roleID = NumberUtils.toInt(request.getParameter("role"));
int organID = NumberUtils.toInt(request.getParameter("organ"));
//是否忽略警告
boolean ignoreWarning = BooleanUtils.toBoolean(request.getParameter("ignore"));
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile importFile = multipartRequest.getFile("theFile");
try {
JSONObject json = new JSONObject();
json.put("needConfirm", false);
pw = response.getWriter();
if(importFile == null){
json.put("error", "未读取到文件");
pw.print(json.toString());
}else{
Workbook workbook = null;
String fileName = importFile.getOriginalFilename();
if(StringUtils.isNotBlank(fileName)){
String[] strArr = fileName.split("\\.");
if(strArr != null && strArr.length > 0){
if("xls".equals(strArr[strArr.length - 1])){
workbook = new HSSFWorkbook(importFile.getInputStream());
} else if("xlsx".equals(strArr[strArr.length - 1])){
workbook = new XSSFWorkbook(importFile.getInputStream());
} else {
json.put("error", "无法读取的文件格式");
pw.print(json.toString());
}
}
}

StringBuilder errorReason = new StringBuilder();
if(organID < 0){
json.put("error", "读取不到正确的机构id");
pw.print(json.toString());
}
List<Object[]> objsArrList = readExcel(workbook, organID, roleID);
boolean needConfirm = checkData(objsArrList, roleID, errorReason);
if(errorReason.length() > 0 &&
( (needConfirm && ! ignoreWarning) || !needConfirm )){
json.put("needConfirm", needConfirm);
json.put("error", errorReason.toString().replaceAll("\\[|]", ""));
pw.print(json.toString());
}
userHelper.saveUserObjects(objsArrList);
pw.print("success");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(pw != null){
pw.flush();
pw.close();
}
}
}
/**
* 检查数据
* @param objsArrList 对象数组
* @param errorReason 错误原因
*/
private boolean checkData(List<Object[]> objsArrList, int roleID, StringBuilder errorReason){
//这是省略了检验数据的代码。。。。
}
/**
* 读取excel并且形成对象
* @param workbook 工作簿对象
* @return
*/
private List<Object[]> readExcel(Workbook workbook, int organID, int roleID){
List<Object[]> objsArr = new ArrayList<Object[]>();
//得到工作簿开始解析数据
Sheet sheet = workbook.getSheetAt(0);
//处理数据
}
}

2、采用注解的方式接受jsp传过来的文件

A: jsp 页面不变

B: Controller 层

其实和上面差不多 主要的区别就是在方法里添加了参数,该参数采用注解的方式,前提条件是要求参数名和jsp中的文件的name值必须一致,否则得到的是null;

具体代码:

@Controller
@RequestMapping("/service/userService")
public class LoadUserServiceAction  {

@Reference
UserHelper userHelper;
@Reference
ClassHelper classHelper;
@Reference
InstitutionHelper institutionHelper;

@RequestMapping("/BatchImport")
public void BatchImport(MultipartFile theFile,HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter pw = null;
request.setCharacterEncoding("utf-8");
int roleID = NumberUtils.toInt(request.getParameter("role"));
int organID = NumberUtils.toInt(request.getParameter("organ"));
//是否忽略警告
boolean ignoreWarning = BooleanUtils.toBoolean(request.getParameter("ignore"));
//theFile拿到的就是该文件,然后进行相应的操作
//和上面的一样
}
}
转载请注明博主链接,尊重原创!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: