ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文件下载

2022-06-27 13:04:12  阅读:81  来源: 互联网

标签:文件 File file new 上传 下载 String


文件下载

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));
 }

 

 





标签:文件,File,file,new,上传,下载,String
来源: https://www.cnblogs.com/EthanLoveMath/p/16415729.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有