一个关于上传的问题,老出错~~

佬侽孩 2010-10-10 04:14:41
上传的servlet类:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* 上传的组件,做修改
* @author Administrator
*
*/
public class FileUpload extends HttpServlet {

/**
* Destruction of the servlet. <br>
*/
private String uploadPath = "F:\\Tomcat5.5\\webapps\\TestUploadFile\\images\\item\\"; // 用于存放上传文件的目录
private File tempPath = new File("D:\\addnetFile\\tmp\\"); // 用于存放临时文件的目录

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html; charset=GB2312");
PrintWriter out = res.getWriter();
System.out.println(req.getContentLength());
System.out.println(req.getContentType());
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(tempPath);

ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000);
try {
List fileItems = upload.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator iter = fileItems.iterator();

// 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$";

// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
}
try {

// 保存上传的文件到指定的目录

// 在下文中上传文件至数据库时,将对这里改写
item.write(new File(uploadPath +"/"+ m.group(1)));

out.print(name + "  " + size + "<br>");
} catch (Exception e) {
out.println(e);
}

} else {
throw new IOException("fail to upload");

}
}
}
} catch (IOException e) {
out.println(e);
} catch (FileUploadException e) {
out.println(e);
}

// 保存上传的文件到指定的目录

// 在下文中上传文件至数据库时,将对这里改写

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}


前台测试页面:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<form action="./FileUpload" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
<form action="./servlet/HelloWord" method="post">
<input type="submit"/>
</form>
<form name="uploadform" method="POST" action="./FileUpload" ENCTYPE="multipart/form-data">

<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF">

<tr><td width="100%" colspan="2">

文件1:<input name="x" size="40" type="file">

</td></tr>

<tr><td width="100%" colspan="2">

文件2:<input name="y" size="40" type="file">

</td></tr>

<tr><td width="100%" colspan="2">

文件3:<input name="z" size="40" type="file">

</td></tr>

</table>

<br/><br/>

<table>

<tr><td align="center"><input name="upload" type="submit" value="开始上传"/></td></tr>

</table>

</form>

</body>
</html>



配置文件xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>com.cn.fileupload.FileUpload</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FileUpload</servlet-name>
<url-pattern>/FileUpload</url-pattern>
</servlet-mapping>
</web-app>


上传地址的文件包,都建立好了,但就是出错;一上传就显示 java.io.IOException: fail to upload
不知道咋回事
...全文
376 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
佬侽孩 2010-10-22
  • 打赏
  • 举报
回复
看来是包的问题啦 哎 换个包看看吧~~谢谢楼上的各位同志们啦~~
caofaping 2010-10-20
  • 打赏
  • 举报
回复
楼主,我帮你测试过,但这边没报错,代码一样,就是包不一样,
我用的是
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
我是在struts2中找的包测得。

我看可能是包的问题。
sowuqing 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 slabcde 的回复:]
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField())
[/Quote]
和明显这里没有进去,看看这个方法的返回类型,是不是,如果是文件域就TRUE,不是就FALSE;
那就得把叹号去掉吧。
佬侽孩 2010-10-19
  • 打赏
  • 举报
回复
顶上去 ~~~待解决
佬侽孩 2010-10-16
  • 打赏
  • 举报
回复
呵呵 继续学习中~~·
闻志流e师兄 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kissau 的回复:]
文件类型的问题,根据你的代码 String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
根据你的注释,你想剔除上面的文件类型,但是你的代码似乎是只能上传以上文件类型的文件。
[/Quote]
String regExp = ".+\\\\(.+)$";

// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
Pattern p = Pattern.compile(regExp);
不好意思看错了,可能和文件路径的\\\\有关
闻志流e师兄 2010-10-14
  • 打赏
  • 举报
回复
文件类型的问题,根据你的代码 String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
根据你的注释,你想剔除上面的文件类型,但是你的代码似乎是只能上传以上文件类型的文件。
yudongming 2010-10-14
  • 打赏
  • 举报
回复
学习了
yaoweijq 2010-10-14
  • 打赏
  • 举报
回复
result是怎么得出来的?
[Quote=引用 6 楼 yimu0214 的回复:]
关键 后台没报一点异常;
前台也买你一上传后 页面出现的代码 就是java.io.IOException: fail to upload
用debug测试了下;是这个位置的result为false



Java code

if (result) {
for (int temp = 0; te……
[/Quote]
佬侽孩 2010-10-13
  • 打赏
  • 举报
回复
关键 后台没报一点异常;
前台也买你一上传后 页面出现的代码 就是java.io.IOException: fail to upload
用debug测试了下;是这个位置的result为false


if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
}
try {

// 保存上传的文件到指定的目录

// 在下文中上传文件至数据库时,将对这里改写
item.write(new File(uploadPath +"/"+ m.group(1)));

out.print(name + "  " + size + "<br>");
} catch (Exception e) {
out.println(e);
}




没进这个result中,进行处理;拿这个代码给我同学,测试上传成功,没问题;靠,真没搞懂是咋回事;
以前用的jspsamart的组建 ,上传成功;现在换的这个apache的这个组建,上传就不成功了,驱动也导入进去了的;o(︶︿︶)o 唉

用到的驱动是:
commons-fileupload-1.1.1.jar
commons-io-1.2.jar
yaoweijq 2010-10-12
  • 打赏
  • 举报
回复
贴下完整的异常代码
佬侽孩 2010-10-12
  • 打赏
  • 举报
回复
o(︶︿︶)o 唉 没人顶 自己顶下吧
佬侽孩 2010-10-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 slabcde 的回复:]
1.检查下你getName()方法返回的值是否带路径
2.检查下返回的路径中分隔符是否是\,有可能是/
[/Quote]


getName()名字 不带路劲

路劲中的分隔符是“\”

这是测试结果:但还是报“java.io.IOException: fail to upload”这个错

multipart/form-data; boundary=---------------------------7da1a522602f8
uploadPath=F:\Tomcat5.5\webapps\TestUploadFile\images\item\
Name=bgToday1.gif

  • 打赏
  • 举报
回复
1.检查下你getName()方法返回的值是否带路径
2.检查下返回的路径中分隔符是否是\,有可能是/
  • 打赏
  • 举报
回复
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField())

这里写的是忽略,else分支里不要抛异常就好了

81,122

社区成员

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

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