JSP组件commons-fileupload实现文件上传

 更新时间:2021年09月01日 15:32:56   投稿:lijiao  
这篇文章主要为大家详细介绍了JSP组件commons-fileupload实现文件上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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处理上传请求

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • jsp连接数据库大全

    jsp连接数据库大全

    jsp连接数据库大全...
    2006-10-10
  • 浅析Java中Data类的应用

    浅析Java中Data类的应用

    浅析Java中Data类的应用...
    2006-10-10
  • Js实现Base64编码与解码

    Js实现Base64编码与解码

    Base64其实是一种简单的置换加密方式,但是BASE64的用处往往并不是为了防止信息泄露,而且为了方便传输,想要了解Base64编码、解码的童鞋可以进来了解一下。
    2016-10-10
  • JSP向后台传递参数的四种方式总结

    JSP向后台传递参数的四种方式总结

    下面小编就为大家带来一篇JSP向后台传递参数的四种方式总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • JSP对浏览器发送来的数据进行重新编码的两种方式

    JSP对浏览器发送来的数据进行重新编码的两种方式

    使用JSP操作中文时,经常会出现一些乱码问题。这里,我们只谈一下对浏览器发送来的数据进行重新编码时的编码方式。众所周知,要对浏览器发送来的数据进行重新编码,只需要一个语句就可以了,很简单
    2013-09-09
  • JSP 动态树的实现

    JSP 动态树的实现

    在实际的项目开发中有很多是有用到动态树,所谓的动态树就是可以支持无限级子节点。今天我就去给大家实际演示一下动态树如何实现。
    2009-04-04
  • 通过spring用beanshell实现java接口示例

    通过spring用beanshell实现java接口示例

    这篇文章主要介绍了通过spring用beanshell实现java接口示例,需要的朋友可以参考下
    2014-03-03
  • 详解Spring的核心机制依赖注入

    详解Spring的核心机制依赖注入

    这篇文章主要介绍了详解Spring的核心机制依赖注入的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • Spring AOP代理详细介绍

    Spring AOP代理详细介绍

    这篇文章主要介绍了Spring AOP代理详细介绍的相关资料,需要的朋友可以参考下
    2017-02-02
  • jsp学习之scriptlet的使用方法详解

    jsp学习之scriptlet的使用方法详解

    这篇文章主要介绍了jsp学习之scriptlet的使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论