JSP组件commons-fileupload实现文件上传
程序员文章站
2023-11-21 17:22:10
本文实例为大家分享了jsp使用commons-fileupload实现文件上传代码,供大家参考,具体内容如下
1、准备:
将commons-fileupload-1.1...
本文实例为大家分享了jsp使用commons-fileupload实现文件上传代码,供大家参考,具体内容如下
1、准备:
将commons-fileupload-1.1.zip和commons-io-1.1.zip复制到"\web-inf\lib"目录下
2、首先是servlet:fileupload.java
package servlet; import java.io.file; import java.io.ioexception; import java.io.printwriter; import java.util.*; import java.util.regex.pattern; import java.util.regex.matcher; 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; public class fileupload extends httpservlet { private string uploadpath="e:\\addnetfile\\";//要上传文件的目录 private file temppath=new file("e:\\tempfile\\");//存放上传的文件的目录 public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=gb2312"); response.setcharacterencoding("gb2312"); printwriter out=response.getwriter(); out.println("请求内容的长度为:"+request.getcontentlength()); out.println("请求内容的类型为:"+request.getcontenttype()); diskfileitemfactory factory=new diskfileitemfactory(); factory.setrepository(temppath); factory.setsizethreshold(4096); servletfileupload upload=new servletfileupload(factory); upload.setsizemax(1000000); list<?> fileitems=null; try{ fileitems=upload.parserequest(request); iterator<?> iterator=fileitems.iterator(); string regex=".+\\\\(.+)$"; string[] errortype={".exe",".com",".cgi",".asp"}; pattern p=pattern.compile(regex); while(iterator.hasnext()){ fileitem item=(fileitem) iterator.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); if(m.find()){ 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(temppath,m.group(1))); out.println(name+" "+size+"<br/>"); out.println("上传成功"); }catch(exception e){ out.println("333"+e); } } else{ throw new ioexception("fail to upload"); } } } }catch(ioexception e){ out.println("222"+e); } catch(fileuploadexception e1){ e1.printstacktrace(); out.println("111"+e1); } } public void init() throws servletexception { if(!new file(uploadpath).isdirectory()) new file(uploadpath).mkdir(); if(!temppath.isdirectory()) temppath.mkdir(); } public void destroy(){ super.destroy(); } }
3、其次是html:uploadfile.html
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>uploadfilel.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/firstjsp/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="/firstjsp/servlet/fileupload" method="post" enctype="multipart/form-data" name="uploadform"> <table> <tr> <td> 文件1:<input type="file" name="x" size="40"/> </td> </tr> <tr> <td> 文件2:<input type="file" name="y" size="40"/> </td> </tr> <tr> <td> 文件3:<input type="file" name="z" size="40"/> </td> </tr> </table> <input type="submit" name="upload" value="开始上传"/> </form> </body> </html>
4、最后是配置web.xml
<servlet> <description>this is the description of my j2ee component</description> <display-name>this is the display name of my j2ee component</display-name> <servlet-name>fileupload</servlet-name> <servlet-class>servlet.fileupload</servlet-class> </servlet> <servlet-mapping> <servlet-name>fileupload</servlet-name> <url-pattern>/servlet/fileupload</url-pattern>
首先运行html,servlet处理上传请求
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: css ie6 ie7 ff的CSS hack使用技巧
下一篇: jsp测试函数的运行速度方法