转载请注明出处:http://blog.csdn.net/jevonsCSDN/article/details/60575575 【Jevons’Blog】
案发现场
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String fileUpload(HttpServletRequest request) {
String result = "";
/*
* response.setCharacterEncoding("UTF-8");
* response.setContentType("application/json;charset=utf-8");
*/
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String path = request.getSession().getServletContext().getRealPath("");
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
System.out.println("表单的参数名称:" + item.getFieldName()
+ ",对应的参数值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上传文件的大小:" + item.getSize());
System.out.println("上传文件的类型:" + item.getContentType());
System.out.println("上传文件的名称:" + item.getName());
File tempFile = new File(item.getName());
String savePath = path
+ File.separator
+ "backend"
+ File.separator
+ "doc"
+ File.separator
+ new SimpleDateFormat("yyyyMMdd-HHmmss")
.format(new Date())
+ tempFile.getName();
item.write(new File(savePath));
// 获取表数据
List<Map<String, String>> list = ExcelUtil
.readExcelOfTeachers(savePath);
// 对表数据进行迭代筛查
Iterator<Map<String, String>> iterator = list
.iterator();
// 备份遍历的前一个数据元
Map<String, String> oldObj = null;
// 利用HashSet过滤表数据,查重。
HashSet<String> checkRepeat = new HashSet<>();
while (iterator.hasNext()) {
Map<String, String> next = iterator.next();
// 教师名称、工号、手机号码、账号和密码为空则导入失败
if (next.get(ExcelUtil.TCODE).equals("")
|| next.get(ExcelUtil.TNAME).equals("")
|| next.get(ExcelUtil.TMOBILE).equals("")
|| next.get(ExcelUtil.TACCOUNT).equals("")) {
if (next.get(ExcelUtil.TPASSWORD).equals("")) {
iterator.remove();
} else if (oldObj != null) {
return "\"" + "导入失败,工号为:"
+ oldObj.get(ExcelUtil.TCODE)
+ " 的教师之后存在空数据,请修改后重新提交。" + "\"";
} else {
return "\"" + "导入失败,表中第二行存在空数据,请修改后重新提交。"
+ "\"";
}
} else if (next.get(ExcelUtil.TPASSWORD).equals("")) {
// 若密码为空,则默认将教师工号设置成密码
next.put(ExcelUtil.TPASSWORD,
next.get(ExcelUtil.TCODE));
} else {
// 表中存在重复账号则导入失败
if (checkRepeat.contains(next
.get(ExcelUtil.TACCOUNT))) {
return "\"" + "导入失败,账号 : "
+ next.get(ExcelUtil.TACCOUNT)
+ " 在表中已重复,请修改后重新提交。" + "\"";
}
// 记录非重复账号信息
checkRepeat.add(next.get(ExcelUtil.TACCOUNT));
}
// 备份本次迭代数据
oldObj = next;
}
result = tTeacherService.addTeacherList(list);
} else {
result = "没有选择上传文件!";
}
}
}
} catch (Exception e) {
e.printStackTrace();
result = "上传文件不成功!";
}
System.out.println(result);
return "\"" + result + "\"";
}
解决方法:
在SpringMVC.xml文件中配置StringHttpMessageConverter的被支持的媒体类型。
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
//添加被支持的媒体类型
<property name="supportedMediaTypes">
<list>
<value>text/xml;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
...
</mvc:message-converters>
</mvc:annotation-driven>
附:
1、text/html是html格式的正文,text/html的意思是将文件的content-type设置为text/html的形式。
2、text/plain是无格式正文,text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理。
3、text/xml忽略xml头所指定编码格式而默认采用us-ascii编码。
4、application/xml会根据xml头指定的编码格式来编码。
Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据,MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。