在通过使用fileupload组件上传的过程中,通过自己的调试,总结如下:1)使用之前的准备,我用的是commons-fileupload-1.1-dev.jar和commons-io-1.1-dev.jar。 解释一下:尽管有的资料解释是commons-fileupload-1.0-beta.jar和commons-beanutils.jar,通过调试的结果 显示并不是需要commons-beanutils.jar文件,而是由于在parserequest(request)的类有关继承于diskfileitem类。而他有private org.apache.commons.io.output.deferredfileoutputstream dfos。这样的就必须使用到commons-io-1.1-dev.jar。因此需要导入该包。否则就出classnotfound:.deferredfileoutputstream的错误。
2)由于涉及文件,就涉及到文件系统。然而在java或应用服务器中对于文件系统的访问,就有一定的安全策略。 需要将下列权限添加到您应用程序服务器的安全策略文件中:permission java.io.filepermission "<<all files>>", "read,write,delete";具体是添加到..\bea\weblogic81 erver\lib\weblogic.policy中的.否则会可能出如下异常错误: org.apache.commons.fileupload.fileuploadexception:java.lang.reflect.invocationtargetexceptionatorg.apache.commons.fileupload.fileupload.createitem(fileupload.java:615)atorg.apache.commons.fileupload.fileupload.parserequest(fileupload.java:474)atorg.apache.commons.fileupload.fileupload.parserequest(fileupload.java:355)….
3)对于不同的服务器,在调试的过程中会出各种不一样的结果。这个与具体的服务器有关。
4)由于fileupload在不断的更新版本,它的很多方法已经不推荐使用了(这与该组件的不断的改进有关)。通过对最新的帮助文档和网上的资料写了一个标准的程序如下:fileupload文件:import java.io.ioexception;import java.io.printwriter;
import javax.servlet.servletexception;import javax.servlet.http.httpservlet;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;
import org.apache.commons.fileupload.*;import java.util.*;import java.util.regex.*;import java.io.*;import org.apache.commons.fileupload.servlet.*;import org.apache.commons.fileupload.disk.diskfileitemfactory;
/* * 创建日期 2005-4-10 * * todo 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - java - 代码样式 - 代码模板 */
/** * @author gaolong1 * * todo 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - java - 代码样式 - 代码模板 */public class fileupload extends httpservlet {
/** * destruction of the servlet. <br> */ private string uploadpath = "d:\\addnetfile\\"; // 用于存放上传文件的目录 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(new file("d:\\file\\addnetfile\\temp"));
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("d:\\" + 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="./servlet/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="./servlet/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>注:该代码由部分来自网上!
