欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 23:25:38
一、所需要的包: 1、commons-fileupload-1.2.1.jar: 下载地址 2、commons-io-1.4.jar: 下载地址...

一、所需要的包:
1、commons-fileupload-1.2.1.jar:
下载地址

2、commons-io-1.4.jar:
下载地址

二、注意事项:
form表单里面要加上enctype="multipart/form-data" 

三、代码示例 

1、jsp代码:

<%@ page language="java" contenttype="text/html; charset=utf-8" 
 pageencoding="utf-8"%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>upload</title> 
</head> 
<body> 
<form action="uploadservlet" method="post" enctype="multipart/form-data"> 
 <table> 
 <caption>上传实例</caption> 
 <tr> 
  <td>姓名</td> 
  <td> 
  <input type="text" name="name"> 
  </td> 
 </tr> 
 <tr> 
  <td>年龄</td> 
  <td> 
  <input type="text" name="age"> 
  </td> 
 </tr> 
 <tr> 
  <td>照片</td> 
  <td> 
  <input type="file" name="image"> 
  </td> 
 </tr> 
 <tr> 
  <td></td> 
  <td> 
  <input type="submit" value="提交"> 
  </td> 
 </tr> 
 </table> 
</form> 
</body> 
</html> 

2、uploadservlet代码

package servlet; 
 
import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.file; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.writer; 
import java.util.iterator; 
import java.util.list; 
 
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.fileuploadbase.sizelimitexceededexception; 
import org.apache.commons.fileupload.disk.diskfileitemfactory; 
import org.apache.commons.fileupload.servlet.servletfileupload; 
import org.apache.commons.fileupload.util.streams; 
 
/** 
 * 上传servlet 
 * @author lisanlai 
 * 
 */ 
public class uploadservlet extends httpservlet { 
 private static final long serialversionuid = 1l; 
  
 public uploadservlet() { 
 super(); 
 } 
 
 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 
 this.dopost(request, response); 
 } 
 
 @suppresswarnings("unchecked") 
 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 
  
 response.setcontenttype("text/html"); 
 // 设置字符编码为utf-8, 这样支持汉字显示 
 response.setcharacterencoding("utf-8"); 
 writer o = response.getwriter(); 
  
 /** 
  * 首先判断form的enctype是不是multipart/form-data 
  * 同时也判断了form的提交方式是不是post 
  * 方法:ismultipartcontent(request) 
  */ 
 
 if(servletfileupload.ismultipartcontent(request)){ 
  request.setcharacterencoding("utf-8"); 
  
  // 实例化一个硬盘文件工厂,用来配置上传组件servletfileupload 
  diskfileitemfactory factory = new diskfileitemfactory(); 
  
  //设置文件存放的临时文件夹,这个文件夹要真实存在 
  file filedir = new file("../webapps/fileupload/tmp/"); 
  if(filedir.isdirectory() && filedir.exists()==false){ 
  filedir.mkdir(); 
  } 
  factory.setrepository(filedir); 
  
  //设置最大占用的内存 
  factory.setsizethreshold(1024000); 
  
  //创建servletfileupload对象 
  servletfileupload sfu = new servletfileupload(factory); 
  sfu.setheaderencoding("utf-8"); 
  
  //设置单个文件最大值byte 
  sfu.setfilesizemax(102400000); 
  
  //所有上传文件的总和最大值byte 
  sfu.setsizemax(204800000); 
  
  list<fileitem> items = null; 
  
  try { 
  items = sfu.parserequest(request); 
  }catch (sizelimitexceededexception e) { 
  system.out.println("文件大小超过了最大值"); 
  } catch(fileuploadexception e) { 
  e.printstacktrace(); 
  } 
  
  //取得items的迭代器 
  iterator<fileitem> iter = items==null?null:items.iterator(); 
  
  //图片上传后存放的路径目录 
  file images = new file("d:/upload/images/"); 
  if(images.exists()==false){ 
  images.mkdirs(); 
  } 
  //迭代items 
  while(iter!=null && iter.hasnext()){ 
  fileitem item = (fileitem) iter.next(); 
   
  //如果传过来的是普通的表单域 
  if(item.isformfield()){ 
   system.out.print("普通的表单域:"); 
   system.out.print(new string(item.getfieldname()) + " "); 
   system.out.println(new string(item.getstring("utf-8"))); 
  } 
  //文件域 
  else if(!item.isformfield()){ 
   system.out.println("源图片:" + item.getname()); 
   string filename = item.getname().substring(item.getname().lastindexof("\\")); 
   bufferedinputstream in = new bufferedinputstream(item.getinputstream()); 
   //文件存储在d:/upload/images目录下,这个目录也得存在 
   bufferedoutputstream out = new bufferedoutputstream( 
    new fileoutputstream(new file(images.getabsolutepath()+ filename))); 
   streams.copy(in, out, true); 
   o.write("文件上传成功"); 
  } 
  } 
 }else { 
  system.out.println("表单的enctype 类型错误"); 
 } 
 } 
 
} 

3、web.xml

<?xml version="1.0" encoding="utf-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> 
 <display-name>uploadtest</display-name> 
 <welcome-file-list> 
 <welcome-file>upload.jsp</welcome-file> 
 </welcome-file-list> 
 <servlet> 
 <description></description> 
 <display-name>uploadservlet</display-name> 
 <servlet-name>uploadservlet</servlet-name> 
 <servlet-class>servlet.uploadservlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>uploadservlet</servlet-name> 
 <url-pattern>/uploadservlet</url-pattern> 
 </servlet-mapping> 
</web-app> 

本文已被整理到了《java上传操作技巧汇总》,欢迎大家学习阅读。

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

上一篇: MyBatis的foreach语句详解

下一篇: