欢迎来到 黑吧安全网 聚焦网络安全前沿资讯,精华内容,交流技术心得!

如何通过JSP组件commons-fileupload实现文件上传功能

来源:本站整理 作者:佚名 时间:2016-11-14 TAG: 我要投稿

本文实例为大家分享了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;temperrortype.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+"");
  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
 
 Uploadfilel.html
 
  http-equiv="keywords" content="keyword1,keyword2,keyword3">
  http-equiv="description" content="this is my page">
  http-equiv="content-type" content="text/html; charset=UTF-8">
 
 -->
 
 
 
  action="/Firstjsp/servlet/FileUpload" method="post" enctype="multipart/form-data" name="form1">
  type="file" name="file"/>
  type="submit" name="submit" value="upload"/>
 
  action="/Firstjsp/servlet/FileUpload" method="post" enctype="multipart/form-data" name="uploadform">
 
 
 
  文件1: type="file" name="X" size="40"/>
 
 
 
 
  文件2: type="file" name="Y" size="40"/>
 
 

[1] [2]  下一页

【声明】:黑吧安全网(http://www.myhack58.com)登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们,联系邮箱admin@myhack58.com,我们会在最短的时间内进行处理。
  • 最新更新
    • 相关阅读
      • 本类热门
        • 最近下载