一开始因为
高兴一阵,因为有事没有接着把图片上传做了,晚上回来接着做
<form action="/lvban/food/insertFood.htm" id="uploadForm" method="post" enctype="multipart/form-data">
表单提交的是二进制所以request.getparameter()取不到值,好百度下发现如下可行
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = null;
items = upload.parseRequest(request);
Iterator itrTwo = items.iterator();
String title = "";
String city = "";
String address = "";
String price = "";
String date = "";
String oneword = "";
while (itrTwo.hasNext()) {
FileItem item = (FileItem) itrTwo.next();
if (item.isFormField()) {
if (item.getFieldName().equals("title")) {
title = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}else if(item.getFieldName().equals("cmbCity")){
city = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}else if(item.getFieldName().equals("address")){
address = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}else if(item.getFieldName().equals("price")){
price = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}else if(item.getFieldName().equals("date")){
date = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}else if(item.getFieldName().equals("oneword")){
oneword = new String(item.getString().getBytes("iso8859-1"), "utf-8");
}
}
}
高兴一阵,因为有事没有接着把图片上传做了,晚上回来接着做
结果gg。用spring mvc注解上传的方式各种不行,
一开始报The request sent by the client was syntactically incorrect这个错,以为参数有问题,名字该来该去,控制器接受的类型也改来改去
呵呵最后摸索了一阵发现原来配置文件少了一个bean类
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
spring mvc配置文件上传的类
好了以为可以,然并卵,原来的需要传文字的其他参数都取不到值了..各种爆炸
然后认怂,用spring mvc绑定对象参数的方式,苦难缺还没有结束
乱码,烦的一笔,照着百度在web.xml加了如下代码
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
结果还不行,debug了一下,发现后台接收已经没问题数据库问题然后加了这个?nuseUnicode=true&characterEncoding=UTF-8终于搞定
还是水平太差