文件下载 /上传

文件下载

ResponseEntity

小文件采取一次性返回到浏览器

     @RequestMapping("down")
     @ResponseBody
     public ResponseEntity m4(HttpSession session) throws IOException {
         //创建上下文域
         ServletContext servletContext = session.getServletContext();
         //获取资源路径 webapp/img/11.jpg
         String realPath = servletContext.getRealPath("/img/11.jpg");
         File file = new File(realPath);
         //获取文件名称给下载时用 并且名称包含后缀 .jpg
         String name = file.getName();
         FileInputStream in = new FileInputStream(file);
         byte[] bytes = new byte[in.available()];
         //流文件存进数组
         in.read(bytes);
         HttpHeaders httpHeaders = new HttpHeaders();
         //设置响应头的下载方式: 附件下载 下载文件的名字
         httpHeaders.add("Content-Disposition","attachment;filename="+ URLEncoder.encode(name,"UTF-8"));
         //设置相应头状态码
         HttpStatus statusCode = HttpStatus.OK;
         //创建 ResponseEntity对象
         ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, httpHeaders, statusCode);
         in.close();
         return  responseEntity;
    }
 }

 

ServletOutputStream

大文件持续下载

     @ResponseBody
     @RequestMapping("down")
     public void m5(HttpServletResponse response, HttpSession session) throws IOException {
 
         ServletContext servletContext = session.getServletContext();
         String realPath = servletContext.getRealPath("/img/终结者-1984_BD国英双语中英双字.mp4");
         File file = new File(realPath);
         String fileName = file.getName();
         FileInputStream inputStream = new FileInputStream(file);
 
         response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
         ServletOutputStream outputStream = response.getOutputStream();
         byte[] bytes = new byte[1024];
         int len;
         while ((len = inputStream.read(bytes)) > 0) {
             outputStream.write(bytes, 0, len);
        }
         inputStream.close();
         outputStream.close();
    }
 }

 

文件上传

MultipartFile 实现类 CommonsMultipartFile

 public String getOriginalFilename()   获取上传文件 name.xxx  包含后缀
 public void transferTo(File dest)   上传文件到服务器具体位置

 

案例 (SpringMVC 环境)

 pom:

 <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.3</version>
 </dependency>

jsp页面
 <form action="" method="post" enctype="multipart/form-data">
     <input type="file" name="file">
     <input type="submit">
 </form>

Spring容器配置文件
 <!--配置文件上传解析器 将上传文件封装成Multipart对象  id必须要有-->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

大小文件都可以 测试4G以上文件都可以

 @RequestMapping("up")
 public void m5(MultipartFile file,HttpSession session) throws IOException {
 
     //获取服务器存放上传文件的目录
     String realPath = session.getServletContext().getRealPath("/img");
     //获取上传文件,包含后缀 name.mp4  
     String filename = file.getOriginalFilename();
     //拼接存储路径   img.xxx.mp4   File.separator:分隔符: .
     String s = realPath + File.separator + filename;
     file.transferTo(new File(s));
 }

上传同名文件处理

默认上传同名文件会自动覆盖

使用UUID替换文件名逗号之前的部分

 //文件上传
 @RequestMapping("up")
 public void m5(MultipartFile file, HttpSession session) throws IOException {
 
     String realPath = session.getServletContext().getRealPath("/img");
     //获取上传文件名称,包含后缀 name.mp4
     String filenameOld = file.getOriginalFilename();
     //分离出上传文件后缀 该后缀包含逗号 .mp4
     String suffixName = filenameOld.substring(filenameOld.lastIndexOf("."));
     String uuid = UUID.randomUUID().toString();
     String filename = uuid + suffixName;
     //拼接存储路径   img.xxx.mp4   File.separator:分隔符: .
     String s = realPath + File.separator + filename;
     file.transferTo(new File(s));
 }

 

 





posted @   __Ethan  阅读(162)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示