导包
| |
| <dependency> |
| <groupId>commons-io</groupId> |
| <artifactId>commons-io</artifactId> |
| <version>2.6</version> |
| </dependency> |
| <dependency> |
| <groupId>commons-fileupload</groupId> |
| <artifactId>commons-fileupload</artifactId> |
| <version>1.4</version> |
| </dependency> |
前端
| <form action="${pageContext.request.contextPath}/upload.do" enctype="multipart/form-data" method="post"> |
| 用户名: <input type="text" name="username"><br> |
| <input type="file" name="file1"><br> |
| <input type="file" name="file2"><br> |
| <input type="submit" value="提交"> | <input type="reset" value="重置"> |
| |
注意这里的enctype="multipart/form-data"一定要写(加密类型),还有请求方式要为POST
servlet
| |
| package com.Google.FileUp; |
| |
| |
| import org.apache.commons.fileupload.FileItem; |
| import org.apache.commons.fileupload.FileUploadException; |
| import org.apache.commons.fileupload.ProgressListener; |
| import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| |
| |
| import javax.servlet.ServletException; |
| import javax.servlet.http.HttpServlet; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import java.io.File; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.util.List; |
| import java.util.UUID; |
| |
| public class FileupServlet extends HttpServlet { |
| |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| |
| } |
| |
| |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| try { |
| |
| if (!ServletFileUpload.isMultipartContent(req)) { |
| return; |
| } |
| |
| |
| String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload"); |
| File uploadFile = new File(uploadPath); |
| if (!uploadFile.exists()) { |
| uploadFile.mkdir(); |
| } |
| |
| |
| |
| |
| |
| String temPath = this.getServletContext().getRealPath("/WEB-INF/tem"); |
| File temFlie = new File(temPath); |
| if (!temFlie.exists()) { |
| temFlie.mkdir(); |
| } |
| |
| |
| DiskFileItemFactory factory = new DiskFileItemFactory(); |
| |
| factory.setSizeThreshold(1024 * 1024); |
| |
| factory.setRepository(temFlie); |
| |
| ServletFileUpload fileUpload = new ServletFileUpload(factory); |
| |
| fileUpload.getProgressListener( |
| |
| |
| |
| |
| ); |
| |
| fileUpload.setHeaderEncoding("UTF-8"); |
| |
| fileUpload.setFileSizeMax(1024 * 1024 * 10); |
| |
| fileUpload.setSizeMax(1024 * 1024 * 10); |
| |
| |
| |
| List<FileItem> fileItems = fileUpload.parseRequest(req); |
| for (FileItem fileItem : fileItems) { |
| |
| if(fileItem.isFormField()){ |
| String name = fileItem.getFieldName(); |
| String value = fileItem.getString("UTF-8"); |
| System.out.println(name+value); |
| }else{ |
| |
| |
| |
| String upLoadName = fileItem.getFieldName(); |
| System.out.println("fileName:"+upLoadName); |
| |
| if(upLoadName.trim().equals("")||upLoadName==null){ |
| continue; |
| } |
| |
| String fileName = upLoadName.substring(upLoadName.lastIndexOf("/") + 1); |
| |
| String fileExtName = upLoadName.substring(upLoadName.lastIndexOf(".") + 1); |
| |
| |
| |
| |
| |
| String uuidPath = UUID.randomUUID().toString(); |
| |
| |
| String realPath = uploadPath+"/"+uuidPath; |
| File realPathFile = new File(realPath); |
| |
| if(!realPathFile.exists()){ |
| realPathFile.mkdir(); |
| } |
| |
| |
| InputStream inputStream = fileItem.getInputStream(); |
| |
| FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName); |
| |
| byte[] buffer = new byte[1024 * 1024]; |
| |
| int i =0; |
| while((i=inputStream.read(buffer))>0){ |
| fos.write(buffer,0,i); |
| } |
| fos.close(); |
| inputStream.close(); |
| } |
| } |
| |
| } catch (FileUploadException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术