当前位置: 代码迷 >> Java Web开发 >> 求教一个小疑点
  详细解决方案

求教一个小疑点

热度:115   发布时间:2016-04-17 00:07:30.0
求教一个小问题
<form method="post" action="uploadConfirm.jsp" enctype="multipart/form-data">
  <tr bgcolor="d3eaef">
  <td >图书图片</td>
  <td><input type ="file" name ="pic"></input></td>
  </tr>

  <tr bgcolor="d3eaef">
  <td colspan="2" align ="center"> <input name="regsubmit" value="提 &nbsp; 交" type="submit"></input></td>
  </tr>
 </form>
上面是上传图片的jsp页面代码
<%
  request.setCharacterEncoding("gb2312");
  SmartUpload m = new SmartUpload();
  m.initialize(pageContext);//上传初始化
  m.upload();
   
  String image = m.getRequest().getParameter("pic");
   
  m.save("\\ima");
  //准备传
%>这是uploadConfirm.jsp的页面代码,怎么image的值是空值呢?很不明白

------解决方案--------------------
String image = m.getRequest().getParameter("pic");

pic是文件类型的啊,你为什么要这样获取它?

查查SmartUpload的例子吧
------解决方案--------------------
你上个页面中 pic 对应的是一个文件,你想把它转换为String????
------解决方案--------------------
String image = m.getRequest().getParameter("pic");


应该有个 getFileName方法,去查看SmartUpload 详细用法吧
------解决方案--------------------
楼主用 m.getRequest().getParameter("pic");
这是正确的,我这里能获得,为什么你那里不能了!确定smartupload包搞对了?
还有在你的m.upload();
这个之前放上一句up.setCharset("gb2312");,但是我觉得不是这里的错
下面是我的一个商品上传时的例子,你自己看看吧

<%
request.setCharacterEncoding("gb2312");
//创建starup对象
SmartUpload up=new SmartUpload();
//将请求打包(smartupload对象初始化)
up.initialize(pageContext);
//设置上传的文件名的字符编码
up.setCharset("gb2312");
//文件上传到tomcat临时文件夹temp下
up.upload();
//保存到项目的upload文件夹中
//up.save("upload");
//如果是中文的文件可能会不显示,如果保存时先改成英文名字的文件
Files fil=up.getFiles();
File file=fil.getFile(0);
//换英文名字
long time=System.currentTimeMillis();//获得系统当前时间的毫秒数,所以一定是经常变得,以确保文件不重名
String filename=file.getFileName();//获得文件的名字
filename=time+filename.substring(filename.indexOf("."),filename.length());//重新命的文件名
file.saveAs("image/"+filename);

String serialNumber=up.getRequest().getParameter("serialNumber");
String name=up.getRequest().getParameter("name");
String brand=up.getRequest().getParameter("brand");
String model=up.getRequest().getParameter("model");
double price=Double.parseDouble(up.getRequest().getParameter("price"));
String description=up.getRequest().getParameter("description");
// 数据库保存代码

%>
  相关解决方案