当前位置:网站首页>文件上传及拓展
文件上传及拓展
2022-07-29 08:50:00 【不想当个程序员】
<%--表单上传文件
get:上传文件大小有限制
post:没有限制
--%>
<form action="" enctype="multipart/form-data" method="post">
上传用户:<input type="text" name="username"><br>
<p><input type="file"/></p>
<p><input type="file"/></p>
<p><input type="submit">|<input type="reset"> </p>
</form>
public class UploadFileServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = "";
// 判断上传的文件是普通表单 还是带文件表单
if (!ServletFileUpload.isMultipartContent(req)) {
return;//终止方法运行,说明这是一个普通的表单,直接返回
}
// 创建上传文件的保存路径,建议在WEB-INF路径,安全,用户无法直接访问上传的文件;
try {
String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");//获得全局的上下文,真实的地址
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdir();//创建这个目录
}
// 缓存。临时文件
// 临时文件假如超过了预期的大小,我们就把他放到一个临时文件中,过几天自动删除,或者提醒用户转存为永久文件
String tmpPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tmpfile = new File(tmpPath);
if (!tmpfile.exists()) {
tmpfile.mkdir();//创建这个临时目录
}
// 1.创建DiskFileItemFactory对象,处理文件上传路径或者大小限制
DiskFileItemFactory factory = getDiskFileItemfactory(tmpfile);
// 2、获取ServletFileUpload
ServletFileUpload upload = getServletFileUpload(factory);
// 3、处理上传文件
msg = uploadParseRequest(upload, req, uploadPath);
} catch (Exception e) {
e.printStackTrace();
}
req.setAttribute("msg", msg);
req.getRequestDispatcher("info.jsp").forward(req, resp);
}
// 1、处理上传的文件,一般需要通过流来获取,我们可以使用request.getInputStream(),原生态的文件上传流获取,十分麻烦
// 但是我们都建议使用Apach的文件上传组件来实现,common-fileupload,它需要依赖于commons-io组件
// 通过这个工厂设置一个缓冲区, 当上传的文件大于这个缓冲区的时候,将他放到临时文件中
private DiskFileItemFactory getDiskFileItemfactory(File tmpfile) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);//缓存区大小为1M
factory.setRepository(tmpfile);//临时文件保存目录,需要一个File
return factory;
}
// 2、获取ServletFileUpload
private ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setProgressListener(new ProgressListener() {
@Override
// pBytesRead:已经读取到的文件大小
// pContentLength:文件大小
public void update(long pBytesRead, long pContentLength, int PItems) {
System.out.println("总大小:" + pContentLength + "已上传:" + pBytesRead);
}
});
// 处理乱码问题
upload.setHeaderEncoding("UTF-8");
// 设置单个文件的最大值
upload.setFileSizeMax(1024 * 1024 * 10);
// 设置总共能够上传文件的大小
// 1Kb*1024=1M*10=10M
upload.setSizeMax(1024 * 1024 * 10);
return upload;
}
//3、处理上传文件
private String uploadParseRequest(ServletFileUpload upload, HttpServletRequest req, String uploadPath) throws IOException {
// 3、处理上传的文件
//把前端请求解析,封装成一个FileItem对象,需要从ServletFileUpload对象中获取
List<FileItem> fileItems = null;
String msg = "";
try {
fileItems = upload.parseRequest(req);
} catch (FileUploadException e) {
e.printStackTrace();
}
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()) {
// getFileName指的是前端表单控件的name;
String name = fileItem.getFieldName();
String value = fileItem.getString("utf-8");
// =========================处理文件=========================
// 拿到文件的名字
String uploadFileName = fileItem.getName();
// 可能存在文件名不合法的情况
if (uploadFileName.trim().equals("") || uploadFileName == null) {
continue;
}
// 获得上传的文件名 /images/girl/pao.png
String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
// 获得文件的后缀名
String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
/* * 如果文件后缀名fileName 不是我们所需要的 * 就直接return,不处理,告诉用户文件类型不对 * */
// 可以使用UUID(唯一识别的通用码),保证文件名唯一
// UUID.randomUUID(),最忌生成一个唯一识别的通用码;
// 网络传输中的东西 都需要序列化
// POJO,实体类,如果想要在多个电脑上运行,传输--->需要把对象都序列化了
// JNT = Java Native Interface
// implement Serializable:标记接口,JVM--->Java栈 本地方法栈 native--->C++
String uuidPath = UUID.randomUUID().toString();
// ===================== 存放地址完毕======================
// 存到哪?uploadPath
// 文件真实存在的路径 realPath
String realPath = uploadPath + "/" + uuidPath;
// 给每个文件创建一个对应的文件夹
File realPathFile = new File(realPath);
if (!realPathFile.exists()) {
realPathFile.mkdir();
}
// ======================存放地址完毕=====================
// 获得文件上传的流
InputStream inputStream = fileItem.getInputStream();
// 创建一个输出流
// realPath = 真实的文件夹
// 差了一个文件;加上输出文件的名字+"/"+uuidFileName
FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);
// 创建一个缓冲区
byte[] buffer = new byte[1024 * 1024];
// 判是否读取完毕
int len = 0;
// 如果大于0说明还存在数据
while ((len = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
// 关闭流
fos.close();
inputStream.close();
msg = "文件上传成功";
fileItem.delete();//文件上传成功,清除临时文件
// 文件传输完毕
}
}
return msg;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
边栏推荐
- Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
- OSG advanced sequence
- Selenium actual combat case crawling JS encrypted data
- Osgsimplegl3 example analysis
- AI is at the forefront | focusing on natural language processing, machine learning and other fields; From Fudan University, Institute of automation, Chinese Academy of Sciences and other teams
- 2022电工(初级)考题模拟考试平台操作
- 2022电工(初级)考题模拟考试平台操作
- AI application lesson 1: C language Alipay face brushing login
- LeetCode刷题(6)
- CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo
猜你喜欢

Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)

2022年R2移动式压力容器充装考题模拟考试平台操作

Data is the main body of future world development, and data security should be raised to the national strategic level

Clickhouse learning (III) table engine

2022 Shandong Province safety officer C certificate work certificate question bank and answers

Day13: file upload vulnerability

Ar virtual augmentation and reality

Mathematical modeling - Differential Equations

2022 R2 mobile pressure vessel filling test question simulation test platform operation

Day6: use PHP to write file upload page
随机推荐
md
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
Squareline partners with visual GUI development of oneos graphical components
Day15: the file contains the vulnerability range manual (self use file include range)
2022 P cylinder filling test simulation 100 questions simulation test platform operation
2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
SAP sm30 brings out description or custom logical relationship
7.1-default-arguments
Mathematical modeling - Differential Equations
Clickhouse learning (II) Clickhouse stand-alone installation
Opencv cvcircle function
01 knapsack about from two-dimensional optimization to one-dimensional optimization
Personal study notes
多重背包,朴素及其二进制优化
Sword finger offer 26. substructure of tree
Osgsimplegl3 combined with renderdoc tool
MySQL 错误总结
2022电工(初级)考题模拟考试平台操作
Source code compilation pytorch pit