fileupload上传文件,isMultipartContent(request) 老为flase

totsheng 2012-07-31 10:56:14
一个上传文件的功能,老提示isMultipartContent(request) 为flase,请问什么原因?
不罗嗦 直接发部分代码

前台页面代码

<form name="postForm" action="paeaReportExample.do?method=addPaeaReportExample" enctype="multipart/form-data" method="post">
<table bgcolor="ece9d8" width="100%" border="0" cellpadding="1"
cellspacing="1" align="center">
<tr height=25>
<td
style="color:ffffff;background: url(<%=Parameters.pathPrefix%>images/subnav_bkgd.gif)"
bgcolor="#0080c0">
<img style='margin-bottom:-3px' src="<%=Parameters.pathPrefix%>images/tb.gif">
填写案例档案表信息
</td>
<td colspan=4 align="left" style="background: url(<%=Parameters.pathPrefix%>images/subnav_bkgd.gif)"
bgcolor="#0080c0">
<input class="ouu" type=button value=" 保存"
onclick="_submitForm(postForm)"
style="background: url(<%=Parameters.pathPrefix%>images/187_1.gif) no-repeat; background-position:5px 1; background-color: #7eaaed">
<input class="ouu" type=button value=" 返回"
onclick="window.close()"
style="background: url(<%=Parameters.pathPrefix%>images/100_2.gif) no-repeat; background-position:5px 1; background-color: #7eaaed">
</td>

</tr>
<tr height=5><td colspan="4"></td></tr>
<tr>
<td align=right bgcolor="cacaca">
标题:<font color=red>*</font>
</td>
<td bgcolor="dedede" width="200">
<input type="text" name="name" style="width: 160px">
</td>
</tr>
<tr height=5><td colspan="4"> </td></tr>
<tr>
<td align=right bgcolor="cacaca">
选择文件:<font color=red>*</font>
</td>
<td bgcolor="dedede" width="200">
<input type="file" name="upload" size="50">
</td>
</tr>


</table>
</form>


private void upload(HttpServletRequest request,String uploadPath) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
File tmpDir = new File("d://temp"); //初始化上传文件的临时存放目录,必须是绝对路径
try {
System.out.println("---ServletFileUpload.isMultipartContent(request)---"+ServletFileUpload.isMultipartContent(request));
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
//指定在内存中缓存数据大小,单位为byte,这里设为1Mb
factory.setSizeThreshold(1 * 1024 * 1024);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
factory.setRepository(tmpDir);
ServletFileUpload sfu = new ServletFileUpload(factory);
// 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb
sfu.setFileSizeMax(5 * 1024 * 1024);
//指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb
sfu.setSizeMax(10 * 1024 * 1024);
sfu.setHeaderEncoding("UTF-8"); //设置编码,因为我的jsp页面的编码是utf-8的
FileItemIterator fii = sfu.getItemIterator(request);// 解析request请求
uploadPath = uploadPath + "upload//"; // 选定上传的目录此处为当前目录
if (!new File(uploadPath).isDirectory()){
new File(uploadPath).mkdirs(); //选定上传的目录此处为当前目录,没有则创建
}

int index = 0;
while (fii.hasNext()) {
FileItemStream fis = fii.next();// 从集合中获得一个文件流
if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
String fileName = fis.getName().substring(
fis.getName().lastIndexOf("."));// 获得上传文件的文件名
fileName = sdf.format(new Date())+"-"+index+fileName;
BufferedInputStream in = new BufferedInputStream(fis.openStream());
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + "//" + fileName)));
Streams.copy(in, out, true); // 开始把文件写到你指定的上传文件夹
index++;
}
}

}
} catch (Exception e) {
e.printStackTrace();
}
}

...全文
3709 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bailtng 2013-08-19
  • 打赏
  • 举报
回复
我也遇到同样的问题了,楼主后来是怎么解决的?
wang1083292853 2013-03-27
  • 打赏
  • 举报
回复
if (!ServletFileUpload.isMultipartContent(request))
tianma630 2013-03-27
  • 打赏
  • 举报
回复
lz用的是springmvc?不要意思 这种方式的上传文件只能在servlet实现 struts啥的都不行
totsheng 2012-07-31
  • 打赏
  • 举报
回复
有人帮忙吗? 在线等呢。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧