当前位置: 移动技术网 > IT编程>开发语言>Java > Servlet实现多文件上传功能

Servlet实现多文件上传功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、servlet实现文件上传,需要添加第三方提供的jar包

下载地址:
1) commons-fileupload-1.2.2-bin.zip : 点击
2) commons-io-2.3-bin.zip :  点击

接着把这两个jar包放到 lib文件夹下:

二、文件上传的表单提交方式必须是post方式

编码类型:enctype="multipart/form-data",默认是 application/x-www-form-urlencoded
比如:<form action="fileupload"enctype="multipart/form-data"method="post">

三、举例

1.fileupload.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" > 
 
 <title>my jsp 'fileupload.jsp' starting page</title> 
 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="this is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > 
 --> 
 
 </head> 
 
 <body> 
 <!-- enctype 默认是 application/x-www-form-urlencoded --> 
 <form action="fileupload" enctype="multipart/form-data" method="post" > 
  
  用户名:<input type="text" name="usename"> <br/> 
  上传文件:<input type="file" name="file1"><br/> 
  上传文件: <input type="file" name="file2"><br/> 
  <input type="submit" value="提交"/> 
 
 </form> 
 
 
 
 </body> 
</html> 

2.实际处理文件上传的 fileupload.java

package com.servlet.fileupload; 
 
import java.io.file; 
import java.io.*; 
import java.io.ioexception; 
import java.io.printwriter; 
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.disk.diskfileitemfactory; 
import org.apache.commons.fileupload.servlet.servletfileupload; 
 
/** 
 * 
 * @author administrator 
 * 文件上传 
 * 具体步骤: 
 * 1)获得磁盘文件条目工厂 diskfileitemfactory 要导包 
 * 2) 利用 request 获取 真实路径 ,供临时文件存储,和 最终文件存储 ,这两个存储位置可不同,也可相同 
 * 3)对 diskfileitemfactory 对象设置一些 属性 
 * 4)高水平的api文件上传处理 servletfileupload upload = new servletfileupload(factory); 
 * 目的是调用 parserequest(request)方法 获得 fileitem 集合list , 
 * 
 * 5)在 fileitem 对象中 获取信息, 遍历, 判断 表单提交过来的信息 是否是 普通文本信息 另做处理 
 * 6) 
 * 第一种. 用第三方 提供的 item.write( new file(path,filename) ); 直接写到磁盘上 
 * 第二种. 手动处理 
 * 
 */ 
public class fileupload extends httpservlet { 
 
 public void dopost(httpservletrequest request, httpservletresponse response) 
  throws servletexception, ioexception { 
  
 request.setcharacterencoding("utf-8"); //设置编码 
  
 //获得磁盘文件条目工厂 
 diskfileitemfactory factory = new diskfileitemfactory(); 
 //获取文件需要上传到的路径 
 string path = request.getrealpath("/upload"); 
  
 //如果没以下两行设置的话,上传大的 文件 会占用 很多内存, 
 //设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件 的目录不同 
 /** 
  * 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上, 
  * 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem 格式的 
  * 然后再将其真正写到 对应目录的硬盘上 
  */ 
 factory.setrepository(new file(path)); 
 //设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室 
 factory.setsizethreshold(1024*1024) ; 
  
 //高水平的api文件上传处理 
 servletfileupload upload = new servletfileupload(factory); 
  
  
 try { 
  //可以上传多个文件 
  list<fileitem> list = (list<fileitem>)upload.parserequest(request); 
  
  for(fileitem item : list) 
  { 
  //获取表单的属性名字 
  string name = item.getfieldname(); 
   
  //如果获取的 表单信息是普通的 文本 信息 
  if(item.isformfield()) 
  {   
   //获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的 
   string value = item.getstring() ; 
   
   request.setattribute(name, value); 
  } 
  //对传入的非 简单的字符串进行处理 ,比如说二进制的 图片,电影这些 
  else 
  { 
   /** 
   * 以下三步,主要获取 上传文件的名字 
   */ 
   //获取路径名 
   string value = item.getname() ; 
   //索引到最后一个反斜杠 
   int start = value.lastindexof("\\"); 
   //截取 上传文件的 字符串名字,加1是 去掉反斜杠, 
   string filename = value.substring(start+1); 
   
   request.setattribute(name, filename); 
   
   //真正写到磁盘上 
   //它抛出的异常 用exception 捕捉 
   
   //item.write( new file(path,filename) );//第三方提供的 
   
   //手动写的 
   outputstream out = new fileoutputstream(new file(path,filename)); 
   
   inputstream in = item.getinputstream() ; 
   
   int length = 0 ; 
   byte [] buf = new byte[1024] ; 
   
   system.out.println("获取上传文件的总共的容量:"+item.getsize()); 
 
   // in.read(buf) 每次读到的数据存放在 buf 数组中 
   while( (length = in.read(buf) ) != -1) 
   { 
   //在 buf 数组中 取出数据 写到 (输出流)磁盘上 
   out.write(buf, 0, length); 
    
   } 
   
   in.close(); 
   out.close(); 
  } 
  } 
  
  
  
 } catch (fileuploadexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
 catch (exception e) { 
  // todo auto-generated catch block 
  
  //e.printstacktrace(); 
 } 
  
  
 request.getrequestdispatcher("filedemo.jsp").forward(request, response); 
  
 
 } 
 
} 


system.out.println("获取上传文件的总共的容量:"+item.getsize());

3.filedemo.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" > 
 
 <title>my jsp 'filedemo.jsp' starting page</title> 
 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="this is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > 
 --> 
 
 </head> 
 
 <body> 
 
 用户名:${requestscope.usename } <br/> 
 文件:${requestscope.file1 }<br/> 
 ${requestscope.file2 }<br/> 
 <!-- 把上传的图片显示出来 --> 
 <img alt="go" src="upload/<%=(string)request.getattribute("file1")%> " /> 
 
 
 
 </body> 
</html> 

4.结果页面

下载链接:
1)struts2之下载  点击打开
2)struts2之上传  点击打开

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网