java web 文件上传下载

时间:2023-03-08 16:22:59
java web 文件上传下载

文件上传下载案例:

首先是此案例工程的目录结构:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAb8AAAF4CAIAAABLnGApAAAgAElEQVR4nO2dbXRU1aH35/nQ76x17/LervU8q7VPiyhalbY+dHprbHsrVHuvtb1UuLbAbetUpDaKFkHFBAyQopSpgqIICVFe5KUFgTFBwiRgEF+KYEUikYZIEAhJGCEkk3k7z4czc87eZ7+cM+ecyZw58/+tvbpmzuy9z57Y+bH3OTP7H4gBAADIn0CxBwAAACUJ7AkAAHaAPQEAwA4cex49erQeeA/8dwGgKBw9etTcntrn8+LlYRRPFdP/kKWO+u6KPQoAOIg+ero9VXX2XRzquzjUde5zFE8V9b9L38Uhf2sUAgWehf3QZe2pqvPchcuffHYBxcvl3IXLavGrQyFQ4FkMn7hALKfO072X2k/1oZRKOd176XTvJQgUeJPmmiuaa64o9ijch/zE6fbsOvf53zvPk6Vydo3hCIrXSte5z/03CYU9S53mmisy6Qu27TlDiusN82pltGd9ff2Jzy68/8k5Q6mcXaM9bn30uv1PfOeZn125/4nv7H/iO2xllGKVE59dOPHZBQgUOGfd9v3yYqUTVZ1O7BmLxWbMmKEwyNUpamXaNt9W2v85dXu+8/EZQ6mcXaM9bp49NjG4e/nkqxODu6Nzv81WdlwOzLoxcOcq17t1say7M/CLP4/IubR/90wPasW5QFdFX71z1UNamb31T7a7ssShxTcFblp8iP8i7FkU1m3ff/SSIipW7Nlcc0UmfiIdP5GJ/0NdvBuK9cEYpGaqTraJdXvmNWM12vPjU/0Hjp42lMrZNVp5dtKXlk+++oHv/OvyyVc/87Mr2coHjp4+cLSt8oaAxk9WcuuISlvlDfk2ybdQwwsEfrE0v+Yv/yTvJjaL9n+CGTNmiI6w5eNT/c5noHeueujKmjt2vPOG1Qabp+ccqD/KcmjxTUI9wp5eRLXnU89tlhdR8+aaK1Kf709/vj/9+f5U7n/JB6nP92+qHGV9POT/7S3Wt7dydzT3PH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jlJln/6yi3/9JVbrMQT0fYkhabHCRH7HAd4+xYbBNzBmaJS4Ua8ODhp3hEAwAv40J67Nj2hBWyoRbWn+vjQ+22yxoz6ckeZeDeF3pWYk1ykvcAs7w2OpipYSewAABQfH9pz24b5WsCGWlR7qo+3bZgva2zbnvwLoKb21J+TK37YEwDv40N7bn5l4Wf9w2RR7ak+3vzKQlljuT2phXRHOEiu3NnkIiaYSHtErNzDEcOJBXlHAACP4UN7rl+7uLt3uLt3WDPmP33llt88kD24fu1iWWMTe9I3h8KGq53UfR5OMBFrT7I/4jYSJ+8IAOAxfGjPl+uWdPXEu3ri6pRTLa9seUM9+HLdkmIPUL4cx1IdgNLAh/ase2lp57l457n4K1ve0NSpHvnH2XjdS46/5ukY6U+NYE8ASgMf2nPVi8s+OTMkKqteXFbMwWUX6hI9wp4AlAY+tOfKlX+Wl2IPEADgB3xoTwAAGAFgTwAAsAPsCQAAdoA9AQDADsW0Z2dntKqurqIuWlVX19kZLfTpAADARYpmz87OaEU0Oi0abWlpqa9/paIuCoECAEqIkbPnka1ztaIoSlU0Oq0uWheti3ZWRaN106qi06rqzHuhNosb8a9FIlEDAJBjhOx5eMtc+umcirpoS0tLNDqtqq6iqq6ioqqiYlod04749bi6CZEL9qT6zK8L2BMAkMNgz/8P95W+Kn2fPZQAAAAASUVORK5CYII=" alt="" />

处理上传: FileUploadServlet.java

 package fnz.fileUploadTest;

 import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 FileUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//创建 FileItem 对象的工厂
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
//负责处理上传的文件数据,并将表单中每个输入项封装成一个 FileItem 对象中
ServletFileUpload sfu = new ServletFileUpload(fileItemFactory);
//设置单个文件最大10M
sfu.setFileSizeMax(10*1024*1024);
//设置总文件最大100M
sfu.setSizeMax(100*1024*1024);
// 设置编码,相当于:request.setCharacterEncoding("UTF-8");
sfu.setHeaderEncoding("UTF-8");
//判断上传表单是否为multipart/form-data类型
if(sfu.isMultipartContent(request)){
//解析request对象,并把表单中的每一个输入项包装成一个fileItem 对象,并返回一个保存了所有FileItem的list集合
List<FileItem> list = sfu.parseRequest(request);
for (FileItem fileItem : list) {
//判断:是否是普通表单字段
if(fileItem.isFormField()){
String fieldName = fileItem.getFieldName();//name
String value = fileItem.getString();//value
System.out.println(fieldName+" "+value);
}
//否则为上传文件
else{
//String fieldName = fileItem.getFieldName(); // 表单元素名称 name
String name = fileItem.getName(); // 文件名
//String content = fileItem.getString(); // 表单元素名称, 对应的数据 value
//String type = fileItem.getContentType(); // 文件类型
//InputStream in = fileItem.getInputStream(); // 上传文件流
// 获取项目文件夹upload对应服务器上的upload路径
String path = getServletContext().getRealPath("/upload"); //用当前时间+文件名 来命名上传后的文件名
SimpleDateFormat time = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_S");
String str = time.format(new Date());
// b. 与文件名拼接
name = str +"#"+ name; //创建一个文件
File file = new File(path,name);
//上传
fileItem.write(file);
//删除系统产生的临时文件
fileItem.delete();
System.out.println("OK");
}
}
} } catch (Exception e) {
throw new RuntimeException(e);
} //将文件放入request域中
Map<String,String> fileNames = new HashMap<String,String>();
String downFolderPath = getServletContext().getRealPath("/upload");
File file = new File(downFolderPath);
String[] files = file.list();
if(files!=null){
for (String fileItem : files) {
String[] splitNames = fileItem.split("#");
fileNames.put(fileItem, splitNames[1]!=null?splitNames[1]:fileItem);
}
}
request.setAttribute("fileNames",fileNames);
request.getRequestDispatcher("/index.jsp").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} }

处理下载:FileDownLoadServlet.java

 package fnz.fileUploadTest;

 import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class FileDownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 获取用户下载的文件名称(url地址后追加数据,get)
String fileName = request.getParameter("downFileName");
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); // 先获取上传目录路径
String basePath = getServletContext().getRealPath("/upload");
// 获取一个文件流
InputStream in = new FileInputStream(new File(basePath,fileName)); // 如果文件名是中文,需要进行url编码
fileName = URLEncoder.encode(fileName, "UTF-8");
// 设置下载的响应头
response.setContentType("application/x-msdownload");
response.setHeader("content-disposition", "attachment;fileName=" + fileName); // 获取response字节流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1){
out.write(b, 0, len);
}
// 关闭
out.close();
in.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} }

前台操作和显示:index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>FileUpload</title>
</head> <body>
<form action="${pageContext.request.contextPath}/FileUploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file1"><br/>
<input type="submit" value="提交">
</form>
<hr>
已上传的文件,点击下载<br/>
<c:if test="${not empty requestScope.fileNames }">
<c:forEach var="item" items="${requestScope.fileNames}" >
<c:url var="url" value="FileDownLoadServlet">
<c:param name="downFileName" value="${item.key}"></c:param>
</c:url>
<a href="${url }">${item.value }</a><br/>
</c:forEach>
</c:if>
</body>
</html>

文件上传和下载注意事项及解释:

文件上传:

<input type="file">标签用于在web页面中添加文件上传输入项,设置文件上传输入项时须注意:
1、必须要设置input输入项的name属性,否则浏览器将不会发送上传文件的数据。
2、必须把form的enctype属性值设为multipart/form-data.method
     提交方式设为post方式。设置该值后,浏览器在上传文件时,将把文件数据附带在http请求消息体中,并使用MIME协议对上传的文件进行描述,以方便接收方对上传数据进行解析和处理。

使用Commons-fileupload组件实现文件上传

需要导入该组件相应的支撑jar包:Commons-fileupload和commons-io。commons-io 不属于文件上传组件的开发jar文件,但Commons-fileupload 组件从1.1 版本开始,它工作时需要commons-io包的支持。

核心API—DiskFileItemFactory

DiskFileItemFactory 是创建 FileItem 对象的工厂,这个工厂类常用方法:
  public DiskFileItemFactory(int sizeThreshold, java.io.File repository)
  构造函数  
  public void setSizeThreshold(int sizeThreshold)
  设置内存缓冲区的大小,默认值为10K。当上传文件大于缓冲区大小时, fileupload组件将使用临时文件缓存上传文件。
  public void setRepository(java.io.File repository)
  指定临时文件目录,默认值为System.getProperty("java.io.tmpdir")

核心API—ServletFileUpload
  ServletFileUpload 负责处理上传的文件数据,并将表单中每个输入项封装成一个 FileItem 对象中。常用方法有:
  boolean isMultipartContent(HttpServletRequest request)
  判断上传表单是否为multipart/form-data类型
  List parseRequest(HttpServletRequest request)
  解析request对象,并把表单中的每一个输入项包装成一个fileItem 对象,并返回一个保存了所有FileItem的list集合。
  setFileSizeMax(long fileSizeMax)
  设置上传文件的最大值
  setSizeMax(long sizeMax)
  设置上传文件总量的最大值
  setHeaderEncoding(java.lang.String encoding)
  设置编码格式
  setProgressListener(ProgressListener pListener)

上传文件的处理细节

  中文文件乱码问题
    文件名中文乱码问题,可调用ServletUpLoader的setHeaderEncoding方法,或者设置request的setCharacterEncoding属性
  临时文件的删除问题
  由于文件大小超出DiskFileItemFactory.setSizeThreshold方法设置的内存缓冲区的大小时,Commons-fileupload组件将使用临时文件保存上传数据,因此在程序结束时,务必调用FileItem.delete方法删除临时文件。
Delete方法的调用必须位于流关闭之后,否则会出现文件占用,而导致删除失败的情况。

文件存放位置

为保证服务器安全,上传文件应保存在应用程序的WEB-INF目录下,或者不受WEB服务器管理的目录。
为防止多用户上传相同文件名的文件,而导致文件覆盖的情况发生,文件上传程序应保证上传文件具有唯一文件名。
为防止单个目录下文件过多,影响文件读写速度,处理上传文件的程序应根据可能的文件上传总量,选择合适的目录结构生成算法,将上传文件分散存储。

ProgressListener显示上传进度

ProgressListener progressListener = new ProgressListener() {
    public void update(long pBytesRead, long pContentLength, int pItems) {

System.out.println("到现在为止,  " + pBytesRead + " 字节已上传,总大小为 "
              + pContentLength);
    }
};
upload.setProgressListener(progressListener);

以KB为单位显示上传进度

long temp = -1;   //temp注意设置为类变量
long ctemp = pBytesRead /1024;
if (mBytes == ctemp)  
    return;
temp = mBytes;

文件下载

Web应用中实现文件下载的两种方式

超链接直接指向下载资源
程序实现下载需设置两个响应头:
设置Content-Type 的值为:application/x-msdownload。Web 服务器需要告诉浏览器其所输出的内容的类型不是普通的文本文件或 HTML 文件,而是一个要保存到本地的下载文件。
Web 服务器希望浏览器不直接处理相应的实体内容,而是由用户选择将相应的实体内容保存到一个文件中,这需要设置 Content-Disposition 报头。该报头指定了接收程序处理数据内容的方式,在 HTTP 应用中只有 attachment 是标准方式,attachment 表示要求用户干预。在 attachment 后面还可以指定 filename 参数,该参数是服务器建议浏览器将实体内容保存到文件中的文件名称。在设置 Content-Dispostion 之前一定要指定 Content-Type

因为要下载的文件可以是各种类型的文件,所以要将文件传送给客户端,其相应内容应该被当做二进制来处理,所以应该调用response.getOutputStream()方法返回 ServeltOutputStream 对象来向客户端写入文件内容。

byte[] b = new byte[1024];
                int len = -1;
                while ((len = in.read(b)) != -1){
                    out.write(b, 0, len);
                }