两种上传方式都可以
依赖pom.xml
1 2 3 4 5 6 | <!-- https: //mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> |
Spring bean配置
1 2 3 4 5 6 7 8 | <!--文件上传id必须=multipartResolver--> <bean id= "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > <!--请求的编码格式,必须和jsp的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1--> <property name= "defaultEncoding" value= "utf-8" /> <!--上传文件大小上限,单位为字节10485760=10M--> <property name= "maxUploadSize" value= "10485760" /> <property name= "maxInMemorySize" value= "40960" /> </bean> |
代码
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 | @PostMapping( "/article/upload1Handler" ) public String upload1Handler(@RequestParam( "file" ) CommonsMultipartFile file, HttpServletRequest request) throws IOException { String uploadFileName = file.getOriginalFilename(); if ( "" . equals (uploadFileName)){ return "redirect:/article/index" ; } 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(); //文件输入流 BufferedInputStream bis = new BufferedInputStream( is ); OutputStream os = new FileOutputStream( new File(realPath,uploadFileName)); //文件输出流 BufferedOutputStream bos = new BufferedOutputStream(os); byte [] buffer = new byte [1024]; int len = 0; while ((len = is .read(buffer)) != -1) { bos.write(buffer, 0, len); } bis.close(); bos.close(); return "redirect:/article/index" ; } @PostMapping( "/article/upload2Handler" ) public String upload2Handler(@RequestParam( "file" ) CommonsMultipartFile file, HttpServletRequest request) throws IOException { String path = request.getServletContext().getRealPath( "/upload" ); File realPath = new File(path); if (!realPath.exists()) { realPath.mkdir(); } System. out .println( "上传文件地址:" + realPath); //通过CommonsMultipartFile的方法直接写文件 file.transferTo( new File(realPath + "/" + file.getOriginalFilename())); return "redirect:/article/index" ; } |
视图
1 2 3 4 | <form action= "/article/upload2Handler" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "file" /> <button type= "submit" >Submit</button> </form> |
下载文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @PostMapping( "/article/downHandler" ) public String downHandler(HttpServletResponse response, HttpServletRequest request) throws IOException { String path = request.getServletContext().getRealPath( "/upload" ); String fileName = "1.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 is = new FileInputStream(file); OutputStream os = response.getOutputStream(); BufferedInputStream bis = new BufferedInputStream( is ); BufferedOutputStream bos = new BufferedOutputStream(os); byte [] buffer = new byte [1024]; int len = 0; while ((len = is .read(buffer)) != -1) { bos.write(buffer, 0, len); } bis.close(); bos.close(); return "redirect:/article/index" ; } |
下载网络文件:
package com.xcg.webapp.common; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class DownloadUtil { /** * 下载网络文件 * 测试下载文件 https://catalog.s.download.windowsupdate.com/d/msdownload/update/software/crup/2024/10/windows10.0-kb5044619-x64_3ef2399e39c2d708f26d02aeb985c2c7f53f9a2c.cab */ public static void downloadNet(String webUrl) throws Exception{ //总字节数 int byteSum = 0; //每次读取的字节数 int byteRead = 0; //网络地址 URL url = new URL(webUrl); //网络文件名 String fileName = new File(webUrl).getName(); try { URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); FileOutputStream fs = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; while ((byteRead = inStream.read(buffer)) != -1) { byteSum += byteRead; fs.write(buffer, 0, byteRead); } System.out.println("总字节数;" + byteSum); } catch (Exception e) { e.printStackTrace(); throw e; } } }
下载文件的几种方式:https://blog.csdn.net/longzhongxiaoniao/article/details/89295891
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2018-08-09 EntityFramework的linq扩展where