java学习笔记 - web基础八(网页上传下载)

web上传下载基础代码

jsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="http://localhost:8888/09_el_jstl/upload" enctype="multipart/form-data" method="post">
用户名:<input type="text" name="username" value="zhibi"><br/>
头像:<input type="file" name="photo"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>

download:

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
ckage com.study.download;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
* @author 执笔
* @version 1.0
* @date 2022/10/20 22:19
*/
public class Download extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// 1. 获取要下载的文件名
String downloadFileName = "4.jpg";
// 2. 读取要下载的文件内容(通过ServletContext对象进行读取)
ServletContext servletContext = getServletContext();
// MimeType() 数据类型的字符串描述符
String mimeType = servletContext.getMimeType("/file/" + downloadFileName);
// 4. 在回传前 通过响应头告诉客户端返回的数据类型
resp.setContentType(mimeType);

// 5. 告诉客户端收到的数据用于下载(使用响应头)
// 当文件名存在中文时 URLEncoder URL编码(IE浏览器、谷歌浏览器)
// 这里的downloadFileName 文件名可以进行更改
resp.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode("你好.jpg", "UTF-8") );

/**
* / 被服务器解析表示地址http://ip:port/工程名/ 映射 到代码的根目录
* getResourceAsStream 获取资源向流一样返回
*/
InputStream resourceAsStream = servletContext.getResourceAsStream("/file/" + downloadFileName);
// 获取响应的输出流

OutputStream outputStream = resp.getOutputStream();
// 读取输入流中的全部数据 复制给输出流 输出给客户端
IOUtils.copy(resourceAsStream,outputStream);
}
}

upload:

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
64
65
66
67
68
69
package com.study.upload;

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;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
* @author 执笔
* @version 1.0
* @date 2022/10/19 21:51
*/
public class Upload extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

/**
* 获取文件的演示
*/
//System.out.println("文件提交");
//ServletInputStream inputStream = req.getInputStream();
//
//
//byte[] buff = new byte[1024000];
//int read = inputStream.read(buff);
//System.out.println(new String(buff,0,read));

//1. 先判断上传的数据是否多段数据(只有多段数据才是文件上传的)
if (ServletFileUpload.isMultipartContent(req)) {
// 创建fileItemFactory 工厂类实现
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
// 创建用于解析上传数据的工具类ServletFileUpload
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

try {
// 解析上传的数据 得到每一个表单项fileItem
List<FileItem> list = servletFileUpload.parseRequest(req);
//循环判断 每一个表单 是普通类型 还是上传文件
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
//普通表单项
System.out.println("表单项的name属性值" + fileItem.getFieldName());
// 参数UTF-8 解决文件有中文名的乱码问题
System.out.println("表单项的value属性值" + fileItem.getString("UTF-8"));
} else {
//上传文件
System.out.println("表单项的name属性值" + fileItem.getFieldName());
// 参数UTF-8 解决文件有中文名的乱码问题
System.out.println("上传文件名" + fileItem.getName());

fileItem.write(new File("j:\\ATest\\" + fileItem.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}