JavaWeb实现文件的上传与下载

时间:2022-09-22 21:09:56

JavaWeb实现文件的上传与下载,供大家参考,具体内容如下

第一步:导包

导入commons-fileupload-1.3.3.jar和commons-io-2.4.jar两个依赖包

第二步:编写前端页面

1、提交页面 index.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 <form action="${pageContext.request.contextPath }/fileUpload" method="post" enctype="multipart/form-data">
 头像:<input type="file" name="img" accept="image/*"/>
 <input type="submit" value="提交"/>
 </form>
 </body>
</html>

2、结果页面 result.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <center>
 <a href="${pageContext.request.contextPath }/download?filename=${filename}" >
 <img alt="xx" src="${src }">
 </a>
 </center>
</body>
</html>

第三步:编写上传和下载代码

1、上传图片 fileUpload.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cn.yz123123.controller;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
 
@WebServlet("/fileUpload")
@MultipartConfig
public class fileUpload extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doPost(request, response);
 }
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 //String username = request.getParameter("username");
 Part part = request.getPart("img");
 
 //获取文件的真实名称
 String header = part.getHeader("content-disposition");
 String realName = header.substring(header.indexOf("filename=")+10, header.length()-1);
 
 //获取文件自身流
 InputStream inputStream = part.getInputStream();
 
 //获取file真实路径,如果没有则创建
 String dir = request.getServletContext().getRealPath("/file/");
 File dirFile = new File(dir);
 //上面只是实例化了一个对象,并没有真正的创建一个文件夹
 if (!dirFile.exists()) {
 dirFile.mkdirs();
 }
 
 //创建文件对象,并用流的形式写在相应的文件夹中
 File file = new File(dir, realName);
 FileOutputStream fileOutputStream = new FileOutputStream(file);
 
 byte[] buf = new byte[1024];
 int len;
 while((len=inputStream.read(buf))!=-1) {
 fileOutputStream.write(buf, 0, len);
 }
 
 fileOutputStream.close();
 inputStream.close();
 
 //以下为测试
 request.setAttribute("src", request.getContextPath()+"/file/"+realName);
 request.setAttribute("filename", realName);
 request.getRequestDispatcher("/result.jsp").forward(request, response);
 }
 
}

2、下载图片 fileDownload.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cn.yz123123.controller;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet(urlPatterns = "/download")
public class fileDownload extends HttpServlet{
 
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 String filename = req.getParameter("filename");
 
 //获取文件的真实路径
 String filePath = req.getServletContext().getRealPath("/file/"+filename);
 
 FileInputStream fileInputStream = new FileInputStream(filePath);
 
 resp.setCharacterEncoding("UTF-8");
 resp.setHeader("Content-Disposition","attachment;filename="+UUID.randomUUID()+filename);
 
 ServletOutputStream outputStream = resp.getOutputStream();
 
 byte[] buf = new byte[1024];
 int len;
 
 while((len=fileInputStream.read(buf))!=-1) {
 outputStream.write(buf, 0, len);
 }
 outputStream.close();
 fileInputStream.close();
 
 }
 
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 super.doGet(req, resp);
 }
}

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

原文链接:https://blog.csdn.net/weixin_56887067/article/details/115428749