method必须是post,enctype是multipart/formdata。开始我的表单是酱紫的:
<form action="emotionUpload" method="post" enctype="multipart/form-data"> zip文件<input type="file" name="file"> zip门头照<input type="file" name="photo"> 文件名<input type="text" name="fileName"> 详情<input type="text" name="fileDetail"> <input type="submit" value="上传"> </form>
然后纠结了一下午为什么后台获取不到值,我直接用的req.getparameter,弄死获取不到。最后网上一通谷歌,发现酱紫不行。需要在upload的fileitem里面获取,个人认为是表单传的值都被封装成了一个FileItem的list,需要遍历这个list来获取传值。代码如下:
DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024*1000); factory.setRepository(tmpFile); ServletFileUpload upload = new ServletFileUpload(factory); upload.setProgressListener(new ProgressListener(){ public void update(long pBytesRead,long pContentLength,int arg2){ System.out.println("文件大小为:"+pContentLength+",当前处理进度:"+pBytesRead); } }); upload.setHeaderEncoding("UTF-8"); upload.setFileSizeMax(1024*1024); upload.setSizeMax(1024*1024*10); List<FileItem> list = upload.parseRequest(req); for(FileItem item : list){ //如果fileitem中封装的是普通输入项的数据 if(item.isFormField()){ String name = item.getFieldName(); //解决普通输入项的数据的中文乱码问题 String value = item.getString("UTF-8"); //value = new String(value.getBytes("iso8859-1"),"UTF-8"); System.out.println(name + "=" + value); if(name.equals("fileName")){ strName = value; } else{ strDetail = value; } }else{//如果fileitem中封装的是上传文件
然后又遇到傻逼问题,中文传值传过来是乱码。修改了jsp的编码,改成gb2312不行。然后改tomcat的编码uriencoding改成utf-8不行。现在还没找到解决办法