文件上传
导包
| <dependencies> |
| <dependency> |
| <groupId>commons-fileupload</groupId> |
| <artifactId>commons-fileupload</artifactId> |
| <version>1.4</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| <version>4.0.1</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>servlet-api</artifactId> |
| <version>2.5</version> |
| </dependency> |
| </dependencies> |
文件上传前端
- multipart/form-data:这种编码方式会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数中,不会对字符编码。
- 请求方式必须要以post方式
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>$Title$</title> |
| </head> |
| <body> |
| <form action="/upload2" enctype="multipart/form-data" method="post"> |
| <input type="file" name="file"/> |
| <input type="submit" value="upload"> |
| </form> |
| </body> |
| </html> |
方式一
| |
| @RequestMapping("/upload") |
| public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { |
| |
| |
| String uploadFileName = file.getOriginalFilename(); |
| |
| |
| if ("".equals(uploadFileName)) { |
| return "redirect:/index.jsp"; |
| } |
| System.out.println("上传文件名 : " + uploadFileName); |
| |
| |
| String path = request.getServletContext().getRealPath("/upload"); |
| |
| File realPath = new File(path); |
| if (!realPath.exists()) { |
| realPath.mkdir(); |
| } |
| System.out.println("上传文件保存地址:" + realPath); |
| |
| InputStream is = file.getInputStream(); |
| OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); |
| |
| |
| int len = 0; |
| byte[] buffer = new byte[1024]; |
| while ((len = is.read(buffer)) != -1) { |
| os.write(buffer, 0, len); |
| os.flush(); |
| } |
| os.close(); |
| is.close(); |
| return "redirect:/index.jsp"; |
| } |
方式二
| |
| |
| |
| @RequestMapping("/upload2") |
| public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { |
| |
| |
| String path = request.getServletContext().getRealPath("/upload2"); |
| File realPath = new File(path); |
| if (!realPath.exists()){ |
| realPath.mkdir(); |
| } |
| |
| System.out.println("上传文件保存地址:"+realPath); |
| |
| |
| file.transferTo(new File(realPath +"/"+ file.getOriginalFilename())); |
| |
| return "redirect:/index.jsp"; |
| } |
文件下载
| @RequestMapping(value="/download") |
| public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{ |
| |
| String path = request.getServletContext().getRealPath("/upload"); |
| String fileName = "img.png"; |
| |
| |
| response.reset(); |
| response.setCharacterEncoding("UTF-8"); |
| response.setContentType("multipart/form-data"); |
| |
| response.setHeader("Content-Disposition", |
| "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8")); |
| |
| File file = new File(path,fileName); |
| |
| InputStream input=new FileInputStream(file); |
| |
| OutputStream out = response.getOutputStream(); |
| |
| byte[] buff =new byte[1024]; |
| int index=0; |
| |
| while((index= input.read(buff))!= -1){ |
| out.write(buff, 0, index); |
| out.flush(); |
| } |
| out.close(); |
| input.close(); |
| return null; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?