今天遇到一个关于文件上传的问题,但是上传文件的同时还要获取另外的表单数据,这个些上传文件的操作和表单数据获取等操作都是在servlet里面处理的。
jspSmartUpload的下载地址:http://download.csdn.net/source/2100914
用于上传数据的表单:
<form name="form1" method="post" action="servlet/Upfile" enctype="multipart/form-data">
<p>请输入手机号</p>
<p>
<input type="text" name="phone" value="1234567890"/>
</p>
<p>图片上传(仅现于*.gif和*.jpg文件)</p>
<p>
<input type="file" name="file1"/>
</p>
<p>
<input type="submit" name="submit" value="· 提交 ·"/>
</p>
</form>
提交的后台的servlet
SmartUpload su =new SmartUpload();
su.initialize(this.getServletConfig(), request, response);
String realPath = this.getServletContext().getRealPath("");
String path=realPath+"/images";
su.setAllowedFilesList("gif,jpg");
su.upload();
int count=su.save(path);
msdnid=su.getRequest().getParameter("phone");//注意这是接收表单传过来的参数
System.out.println("msdnid="+msdnid);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
特别说明:用于接收表单参数的语句一定要放在su.upload();语句的后面,也就是说要在文件上传后再接收,否则
不管你怎么试接收到的结果总是"null".
这是我在苦苦试了一天在晕到前的2分钟试出来的,真的不容易呀!!!!!
在上述问题得到解决后,当所传的参数为汉字时则接收到的全是乱码,经过反复实验将表单页面的编码格式设成"gb2312"就解决了这个问题.