• Javaweb项目中文件的上传与下载


    文件上传:将本地文件通过流的方式上传到服务器,常用的文件上传方法有很多,本次主要介绍的文件上传技术是FileUpLoad.文件上传三要素:1、表单提交方式为post.  2、需要有属性,name元素和值。3、表单enctype="multipart/form-data"。文件下载:将服务器的文件通过流写到客户端。文件下载的方式主要为超链接下载和手动编写代码方式下载。

    目录

    1、文件上传入门

    2-JS控制多个文件上传

    3-防止上传重名文件覆盖问题

    4-文件下载之超链接方式

    5-文件下载之手动编写Servlet方式

    6-文件下载之防止浏览器不同下载乱码问题

    7-指定目录的文件下载案例


    1、文件上传入门

    首先创建web项目,配置tomcat服务器,导入文件上传所需jar包,具体如下:

    web项目中创建文件上传的upload.jsp页面,具体如下:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: nuist__NJUPT
    4. Date: 2022-08-15
    5. Time: 16:53
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <form action="${pageContext.request.contextPath}/UploadServlet" method="post" enctype="multipart/form-data">
    15. 文件描述:<input type="text" name="info">br>
    16. 文件上传:<input type="file" name="upload">br>
    17. <input type="submit" value="文件上传">
    18. form>
    19. body>
    20. html>

    处理upload.jsp表单提交的Servlet类,具体如下:

    1. import org.apache.commons.fileupload.FileItem;
    2. import org.apache.commons.fileupload.FileUploadException;
    3. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    4. import org.apache.commons.fileupload.servlet.ServletFileUpload;
    5. import javax.servlet.ServletException;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.*;
    10. import java.util.List;
    11. public class UploadServlet extends HttpServlet {
    12. @Override
    13. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. boolean flag = ServletFileUpload.isMultipartContent(req) ;
    15. if(!flag){
    16. //如果表单不是enctype="multipart/form-data"
    17. req.setAttribute("msg","表单设置不正确");
    18. req.getRequestDispatcher("/jsp/upload.jsp").forward(req,resp);
    19. }
    20. try {
    21. //1.创建磁盘文件项工厂
    22. DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    23. diskFileItemFactory.setSizeThreshold(3*1024*1024); //设置缓冲区的大小为3MB
    24. //设置文件的临时路径
    25. String temp = getServletContext().getRealPath("/temp") ;
    26. diskFileItemFactory.setRepository(new File(temp));
    27. //2.创建一个核心解析类
    28. ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory) ;
    29. //设置上传文件的大小为5MB
    30. // fileUpload.setSizeMax(5*1024*1024);
    31. //3.利用核心类解析Request,解析后会得到多个部分,存入list集合中
    32. List list = fileUpload.parseRequest(req) ;
    33. //4.遍历文件项,判断文件项是普通项,还是文件上传项
    34. for(FileItem fileItem : list){
    35. if(fileItem.isFormField()){
    36. //接收普通项的值
    37. String name = fileItem.getFieldName() ;
    38. String value = fileItem.getString("UTF-8") ;
    39. System.out.println(name + " "+ value);
    40. }else{
    41. //文件上传项目
    42. //获得文件上传项的文件名及文件内容
    43. String filename = fileItem.getName() ;
    44. int index = filename.lastIndexOf("\\") ;
    45. if(index != -1){
    46. //老版本浏览器
    47. filename = filename.substring(index+1) ;
    48. }
    49. InputStream is = fileItem.getInputStream() ;
    50. //获得文件上传的磁盘绝对路径
    51. String realPath = getServletContext().getRealPath("/upload");
    52. //创建一个输出流,写入到真实的路径中
    53. OutputStream os = new FileOutputStream(realPath+"/"+filename) ;
    54. //两个流实现对接
    55. int len = 0 ;
    56. byte [] bytes = new byte[1024] ;
    57. while((len=is.read(bytes)) != -1){
    58. os.write(bytes,0,len);
    59. }
    60. is.close();
    61. os.close();
    62. }
    63. // fileItem.delete(); //删除临时文件
    64. }
    65. } catch (FileUploadException e) {
    66. e.printStackTrace();
    67. }
    68. }
    69. @Override
    70. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    71. doGet(req,resp);
    72. }
    73. }

    在web.xml文件中对使用的Servlet的名称及路径进行配置,具体如下:

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <servlet>
    6. <servlet-name>UploadServletservlet-name>
    7. <servlet-class>com.controller.UploadServletservlet-class>
    8. servlet>
    9. <servlet-mapping>
    10. <servlet-name>UploadServletservlet-name>
    11. <url-pattern>/UploadServleturl-pattern>
    12. servlet-mapping>
    13. web-app>

    2-JS控制多个文件上传

    JavaScript控制多个文件上传,在原来的基础上,编写多文件上传页面jsp如下:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: nuist__NJUPT
    4. Date: 2022-08-15
    5. Time: 16:53
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. <script>
    13. function add() {
    14. //获得id为div1的元素
    15. var div1Element = document.getElementById("div1") ;
    16. div1Element.innerHTML += "
      "
      ;
    17. }
    18. function del(who) {
    19. //每次找到父类后将自身移除
    20. who.parentNode.parentNode.removeChild(who.parentNode) ;
    21. }
    22. script>
    23. head>
    24. <body>
    25. <h1>多文件上传h1>
    26. <form action="${pageContext.request.contextPath}/UploadServlet" method="post" enctype="multipart/form-data">
    27. 文件描述:<input type="button" value="添加" onclick="add()">br>
    28. <input type="submit" value="文件上传"><br/>
    29. <div id="div1">
    30. div>
    31. form>
    32. body>
    33. html>

    3-防止上传重名文件覆盖问题

    防止不同浏览器上传重名的文件将原始的文件覆盖掉,故使用工具类生成唯一的文件名,工具类如下:

    1. import java.util.UUID;
    2. /**
    3. * 文件上传的工具类
    4. */
    5. public class UploadUtils {
    6. public static String getUuidFilename(String filename) {
    7. //获得文件的扩展名
    8. int index = filename.lastIndexOf(".");
    9. String extension = filename.substring(index);
    10. return UUID.randomUUID().toString().replace("-", "") + extension;
    11. }
    12. }

    在servlet中使用该工具类对上传文件生成唯一的文件名,具体如下:

    1. import com.utils.UploadUtils;
    2. import org.apache.commons.fileupload.FileItem;
    3. import org.apache.commons.fileupload.FileUploadException;
    4. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    5. import org.apache.commons.fileupload.servlet.ServletFileUpload;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.http.HttpServlet;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.io.*;
    11. import java.util.List;
    12. public class UploadServlet extends HttpServlet {
    13. @Override
    14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. boolean flag = ServletFileUpload.isMultipartContent(req) ;
    16. if(!flag){
    17. //如果表单不是enctype="multipart/form-data"
    18. req.setAttribute("msg","表单设置不正确");
    19. req.getRequestDispatcher("/jsp/upload.jsp").forward(req,resp);
    20. }
    21. try {
    22. //1.创建磁盘文件项工厂
    23. DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    24. diskFileItemFactory.setSizeThreshold(3*1024*1024); //设置缓冲区的大小为3MB
    25. //设置文件的临时路径
    26. String temp = getServletContext().getRealPath("/temp") ;
    27. diskFileItemFactory.setRepository(new File(temp));
    28. //2.创建一个核心解析类
    29. ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory) ;
    30. //设置上传文件的大小为5MB
    31. // fileUpload.setSizeMax(5*1024*1024);
    32. //3.利用核心类解析Request,解析后会得到多个部分,存入list集合中
    33. List list = fileUpload.parseRequest(req) ;
    34. //4.遍历文件项,判断文件项是普通项,还是文件上传项
    35. for(FileItem fileItem : list){
    36. if(fileItem.isFormField()){
    37. //接收普通项的值
    38. String name = fileItem.getFieldName() ;
    39. String value = fileItem.getString("UTF-8") ;
    40. System.out.println(name + " "+ value);
    41. }else{
    42. //文件上传项目
    43. //获得文件上传项的文件名及文件内容
    44. String filename = fileItem.getName() ;
    45. int index = filename.lastIndexOf("\\") ;
    46. if(index != -1){
    47. //老版本浏览器
    48. filename = filename.substring(index+1) ;
    49. }
    50. String filename1 = UploadUtils.getUuidFilename(filename) ;
    51. InputStream is = fileItem.getInputStream() ;
    52. //获得文件上传的磁盘绝对路径
    53. String realPath = getServletContext().getRealPath("/upload");
    54. //创建一个输出流,写入到真实的路径中
    55. OutputStream os = new FileOutputStream(realPath+"/"+filename1) ;
    56. //两个流实现对接
    57. int len = 0 ;
    58. byte [] bytes = new byte[1024] ;
    59. while((len=is.read(bytes)) != -1){
    60. os.write(bytes,0,len);
    61. }
    62. is.close();
    63. os.close();
    64. }
    65. // fileItem.delete(); //删除临时文件
    66. }
    67. } catch (FileUploadException e) {
    68. e.printStackTrace();
    69. }
    70. }
    71. @Override
    72. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    73. doGet(req,resp);
    74. }
    75. }

    4-文件下载之超链接方式

    文件下载方式1:超链接下载方式,下载服务器在download文件夹下的资源,代码如下:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: nuist__NJUPT
    4. Date: 2022-08-15
    5. Time: 20:22
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <h1>文件下载:超链接的方式h1>
    15. <h3><a href="${ pageContext.request.contextPath }/download/bootstrap-3.4.1-dist.zip">bootstrap.zipa>h3>
    16. <h3><a href="${ pageContext.request.contextPath }/download/a.docx">a.docxa>h3>
    17. body>
    18. html>

    5-文件下载之手动编写Servlet方式

    我们使用手动编写Servlet,将资源交给Servlet下载,首先在jsp页面将资源文件转给Servlet,代码如下:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: nuist__NJUPT
    4. Date: 2022-08-15
    5. Time: 20:22
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <h1>文件下载:手动编程下载的方式h1>
    15. <h3><a href="${ pageContext.request.contextPath }/DownloadServlet?filename=bootstrap-3.4.1-dist.zip">bootstrap.zipa>h3>
    16. <h3><a href="${ pageContext.request.contextPath }/DownloadServlet?filename=a.docx">a.docxa>h3>
    17. body>
    18. html>

    然后编写用于处理文件下载的Servlet类,具体如下:

    1. import javax.servlet.ServletException;
    2. import javax.servlet.http.HttpServlet;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.FileInputStream;
    6. import java.io.IOException;
    7. import java.io.InputStream;
    8. import java.io.OutputStream;
    9. public class DownloadServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //接收参数
    13. String filename = request.getParameter("filename") ;
    14. //下载,设置两个头和一个流
    15. //设置Content-type
    16. String type = request.getServletContext().getMimeType(filename) ;
    17. response.setContentType(type);
    18. //设置Content-Disposition
    19. response.setHeader("Content-Disposition","attachment;filename="+filename);
    20. //设置一个代表文件的输入流
    21. String path = request.getServletContext().getRealPath("/download" ) ;
    22. InputStream is = new FileInputStream(path+"/"+filename) ;
    23. OutputStream os = response.getOutputStream() ;
    24. //两个流的对接
    25. int len = 0 ;
    26. byte [] bytes = new byte[1024] ;
    27. while((len=is.read(bytes)) != -1){
    28. os.write(bytes,0,len);
    29. }
    30. is.close();
    31. os.close();
    32. }
    33. @Override
    34. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    35. doGet(req,resp);
    36. }
    37. }

    最后需要在web.xml文件中配置Servlet,具体如下:

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <servlet>
    6. <servlet-name>DownloadServletservlet-name>
    7. <servlet-class>com.controller.DownloadServletservlet-class>
    8. servlet>
    9. <servlet-mapping>
    10. <servlet-name>DownloadServletservlet-name>
    11. <url-pattern>/DownloadServleturl-pattern>
    12. servlet-mapping>
    13. web-app>

    6-文件下载之防止浏览器不同下载乱码问题

    对于包含中文名称的文件,防止乱码,需要识别浏览器类型,并设置为UTF-8编码,具体的Servlet如下所示:

    1. import com.utils.DownloadUtils;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.*;
    7. import java.net.URLEncoder;
    8. public class DownloadServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    11. //接收参数
    12. String filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8");
    13. //下载,设置两个头和一个流
    14. //设置Content-type
    15. String type = request.getServletContext().getMimeType(filename) ;
    16. response.setContentType(type);
    17. //设置一个代表文件的输入流
    18. String path = request.getServletContext().getRealPath("/download" ) ;
    19. File file = new File(path+"/"+filename);
    20. // 判断浏览器的类型:
    21. String agent = request.getHeader("User-Agent");
    22. if(agent.contains("Firefox")){
    23. // 使用的是Firefox
    24. filename = DownloadUtils.base64EncodeFileName(filename);
    25. }else{
    26. // IE或者其他的浏览器
    27. filename = URLEncoder.encode(filename, "UTF-8");
    28. }
    29. //设置Content-Disposition
    30. response.setHeader("Content-Disposition","attachment;filename="+filename);
    31. InputStream is = new FileInputStream(file) ;
    32. OutputStream os = response.getOutputStream() ;
    33. //两个流的对接
    34. int len = 0 ;
    35. byte [] bytes = new byte[1024] ;
    36. while((len=is.read(bytes)) != -1){
    37. os.write(bytes,0,len);
    38. }
    39. is.close();
    40. os.close();
    41. }
    42. @Override
    43. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    44. doGet(req,resp);
    45. }
    46. }

    当然这个servlet中对于火狐浏览器的编码问题使用了一个工具类,具体如下:

    1. import java.io.UnsupportedEncodingException;
    2. import sun.misc.BASE64Encoder;
    3. public class DownloadUtils {
    4. public static String base64EncodeFileName(String fileName) {
    5. BASE64Encoder base64Encoder = new BASE64Encoder();
    6. try {
    7. return "=?UTF-8?B?"
    8. + new String(base64Encoder.encode(fileName
    9. .getBytes("UTF-8"))) + "?=";
    10. } catch (UnsupportedEncodingException e) {
    11. e.printStackTrace();
    12. throw new RuntimeException(e);
    13. }
    14. }
    15. }

    7-指定目录的文件下载案例

    定义一个jsp页面用于在浏览器显示目录中的所有文件。

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: nuist__NJUPT
    4. Date: 2022-08-16
    5. Time: 9:33
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@page import="java.io.File"%>
    9. <%@page import="java.util.*"%>
    10. <%@ page language="java" contentType="text/html; charset=UTF-8"
    11. pageEncoding="UTF-8"%>
    12. html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    13. <html>
    14. <head>
    15. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    16. <title>Insert title heretitle>
    17. head>
    18. <body>
    19. <h1>树形遍历h1>
    20. <%
    21. // 1.创建一个队列:
    22. Queue<File> queue = new LinkedList<File>();
    23. // 2.先将跟节点入队:
    24. File root = new File("E://resource");
    25. queue.offer(root);
    26. // 判断这个队列是否为空,不为空需要进行遍历:
    27. while(!queue.isEmpty()){
    28. // 将跟节点出队:
    29. File file = queue.poll();
    30. // 获得跟节点下的所有子节点:
    31. File[] files = file.listFiles();
    32. // 遍历所有子节点:
    33. for(File f:files){
    34. // 判断该节点是否为叶子节点:
    35. if(f.isFile()){
    36. %>
    37. <h4><a href="${ pageContext.request.contextPath }/DownloadListServlet?filename=<%=f.getCanonicalPath()%>"><%= f.getName() %>a>h4>
    38. <%
    39. }else{
    40. queue.offer(f);
    41. }
    42. }
    43. }
    44. %>
    45. body>
    46. html>

    手动编写servlet处理文件下载问题,具体如下:
     

    1. import com.utils.DownloadUtils;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.*;
    7. import java.net.URLEncoder;
    8. public class DownloadListServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    11. // 接收参数:
    12. String path = request.getParameter("filename");
    13. if(path != null){
    14. path = new String(path.getBytes("ISO-8859-1"),"UTF-8");
    15. }
    16. File file = new File(path);
    17. // 实现文件下载:设置两个头和一个流:
    18. // 获得文件名
    19. String filename = file.getName();
    20. response.setContentType(getServletContext().getMimeType(filename));
    21. // 设置另一个头:
    22. String agent = request.getHeader("User-Agent");
    23. if(agent.contains("Firefox")){
    24. filename = DownloadUtils.base64EncodeFileName(filename);
    25. }else{
    26. filename = URLEncoder.encode(filename, "UTF-8");
    27. filename = filename.replace("+", " ");
    28. }
    29. response.setHeader("Content-Disposition", "attachment;filename="+filename);
    30. // 设置输入流:
    31. InputStream is = new FileInputStream(file);
    32. OutputStream os = response.getOutputStream();
    33. int len = 0;
    34. byte[] b = new byte[1024];
    35. while((len = is.read(b))!=-1){
    36. os.write(b, 0, len);
    37. }
    38. is.close();
    39. }
    40. @Override
    41. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    42. doGet(req,resp);
    43. }
    44. }

    在web.xml中配置servlet,具体如下:

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <servlet>
    6. <servlet-name>DownloadListServletservlet-name>
    7. <servlet-class>com.controller.DownloadListServletservlet-class>
    8. servlet>
    9. <servlet-mapping>
    10. <servlet-name>DownloadListServletservlet-name>
    11. <url-pattern>/DownloadListServleturl-pattern>
    12. servlet-mapping>
    13. web-app>
  • 相关阅读:
    kotlin完成 Code War 题目 解析分子公式
    【仿美团点餐App】—— 首页(一)
    偷偷盘点一下这几年秋招薪资的变化
    python经典百题之判断星期几
    408计网应用层总结
    【机器学习】模型平移不变性/等变性&归纳偏置&Attention机制
    TeamViewer 可信设备的信任管理
    “桥接模式”和“NAT模式”以及“host-only模式(仅主机模式)”
    pytorch里常用操作(持续更新)
    spark分布式计算框架
  • 原文地址:https://blog.csdn.net/nuist_NJUPT/article/details/126350959