无为清净楼资源网 Design By www.qnjia.com
本文实例为大家分享了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<".+\\\\(.+)$"; 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处理上传请求
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 茵达INDAHKUS《茵 the Blue》[FLAC/分轨][147.57MB]
- 老虎鱼AnneClarkwithUllavanDaelenJustinCiuche-Borderland-HiRes-Files24bit88.2kHz
- 江玲2004-百合盛开2CD[歌林][WAV+CUE]
- 孙露《怕什么孤单3CD》深圳音像[WAV分轨]
- s14全球总决赛目前是什么结果 s14全球总决赛赛程结果图一览
- s2TPA夺冠五人名单都有谁 tpa战队s2夺冠队员名单一览
- s13wbg战队成员都有谁 wbg战队s13成员名单一览
- 网友热议《幻兽帕鲁》停售、赔千万日元:那也赚麻了 不亏
- 这谁顶得住 《最终幻想7:重生》尤菲3D作品穿上终极芙蕾娜衣服
- 外媒称PS5pro违背承诺:《蜘蛛侠2》根本没法4K60帧
- 群星.2007-中文十大金曲30周年纪念专辑【RTHK】【WAV+CUE】
- 杨林.1989-留一点爱来爱自己【综一唱片】【WAV+CUE】
- 南合文斗.2007-陪君醉笑三千尘鸟人唱片】【FLAC+CUE】
- 群星《我们的歌第六季 第1期》[320K/MP3][90.72MB]
- 群星《我们的歌第六季 第1期》[FLAC/分轨][456.01MB]