当前位置:网站首页>文件上传及拓展
文件上传及拓展
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);
}
}
边栏推荐
- 正则表达式校验版本号
- 2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
- Basic shell operations (Part 2)
- 2022 Shandong Province safety officer C certificate work certificate question bank and answers
- Sword finger offer 32 - ii Print binary tree II from top to bottom
- Multiple knapsack, simplicity and binary optimization
- Design of distributed (cluster) file system
- Day4: SQL server is easy to use
- RESTful 风格详解
- Component transfer participation lifecycle
猜你喜欢

Chrony time synchronization

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

MySQL 错误总结

7.2-function-overloading

Leetcode question brushing (6)

Ar virtual augmentation and reality

C language -- 22 one dimensional array

Lesson 3 threejs panoramic preview room case

Mathematical modeling clustering

Flask reports an error runtimeerror: the session is unavailable because no secret key was set
随机推荐
Osgsimplegl3 example analysis
What is the difference between the pre training model and the traditional method in sorting?
(视频+图文)机器学习入门系列-第3章 逻辑回归
2022年山东省安全员C证上岗证题库及答案
access数据库可以被远程访问吗
解决Base64 报错 Illegal base64 character
User identity identification and account system practice
Sword finger offer 50. the first character that appears only once
Cmake setting vs Startup running environment path
Squareline partners with visual GUI development of oneos graphical components
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
C language sorts n integers with pointers pointing to pointers
[C language] DataGridView binding data
Count the list of third-party components of an open source project
Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
Eggjs create application knowledge points
QT version of Snake game project
01背包关于从二维优化到一维
English high frequency suffix