SpringBoot项目实现文件的下载,解决中文文件名变下滑线

转载路径:https://www.cnblogs.com/yaowen/p/8918678.html

                  https://www.jianshu.com/p/c2feb703d21a

一、下载文件的四种方式:Springboot对资源的描述提供了相应的接口,其主要实现类有ClassPathResource、FileSystemResource、UrlResource、ByteArrayResource、

ServletContextResource和InputStreamResource。

  1. ClassPathResource可用来获取类路径下的资源文件。假设我们有一个资源文件test.txt在类路径下,我们就可以通过给定对应资源文件在类路径下的路径path来获取它,new ClassPathResource(“test.txt”)。
  2. FileSystemResource可用来获取文件系统里面的资源。我们可以通过对应资源文件的文件路径来构建一个FileSystemResource。FileSystemResource还可以往对应的资源文件里面写内容,当然前提是当前资源文件是可写的,这可以通过其isWritable()方法来判断。FileSystemResource对外开放了对应资源文件的输出流,可以通过getOutputStream()方法获取到。
  3. UrlResource可用来代表URL对应的资源,它对URL做了一个简单的封装。通过给定一个URL地址,我们就能构建一个UrlResource。
  4. ByteArrayResource是针对于字节数组封装的资源,它的构建需要一个字节数组。
  5. ServletContextResource是针对于ServletContext封装的资源,用于访问ServletContext环境下的资源。ServletContextResource持有一个ServletContext的引用,其底层是通过ServletContext的getResource()方法和getResourceAsStream()方法来获取资源的。
  6. InputStreamResource是针对于输入流封装的资源,它的构建需要一个输入流。

Resource接口中主要定义有以下方法:

  1. exists():用于判断对应的资源是否真的存在。
  2. isReadable():用于判断对应资源的内容是否可读。需要注意的是当其结果为true的时候,其内容未必真的可读,但如果返回false,则其内容必定不可读。
  3. isOpen():用于判断当前资源是否代表一个已打开的输入流,如果结果为true,则表示当前资源的输入流不可多次读取,而且在读取以后需要对它进行关闭,以防止内存泄露。该方法主要针对于InputStreamResource,实现类中只有它的返回结果为true,其他都为false。
  4. getURL():返回当前资源对应的URL。如果当前资源不能解析为一个URL则会抛出异常。如ByteArrayResource就不能解析为一个URL。
  5. getFile():返回当前资源对应的File。如果当前资源不能以绝对路径解析为一个File则会抛出异常。如ByteArrayResource就不能解析为一个File。
  6. getInputStream():获取当前资源代表的输入流。除了InputStreamResource以外,其它Resource实现类每次调用getInputStream()方法都将返回一个全新的InputStream。
  7. 以及一些类似于Java中的File的接口,比如getName,getContenLength等等。

如果需要获取本地文件系统中的指定路径下的文件,有一下几种方式

  1. 通过ResponseEntity<InputStreamResource>实现
  2. 通过写HttpServletResponse的OutputStream实现

     第一种方式通过封装ResponseEntity,将文件流写入body中。这里注意一点,就是文件的格式需要根据具体文件的类型来设置,一般默认为application/octet-stream。文件头中设置缓存,以及文件的名字。文件的名字写入了,都可以避免出现文件随机产生名字,而不能识别的问题。

[python] view plain copy

@RequestMapping(value = "/media", method = RequestMethod.GET)  
    public ResponseEntity<InputStreamResource> downloadFile( Long id)  
            throws IOException {  
        String filePath = "E:/" + id + ".rmvb";  
        FileSystemResource file = new FileSystemResource(filePath);  
        HttpHeaders headers = new HttpHeaders();  
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");  
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));  
        headers.add("Pragma", "no-cache");  
        headers.add("Expires", "0");  
  
        return ResponseEntity  
                .ok()  
                .headers(headers)  
                .contentLength(file.contentLength())  
                .contentType(MediaType.parseMediaType("application/octet-stream"))  
                .body(new InputStreamResource(file.getInputStream()));  
    }  

     第二种方式采用了Java中的File文件资源,然后通过写response的输出流,放回文件。

[python] view plain copy

  1. @RequestMapping(value="/media/", method=RequestMethod.GET)  
        public void getDownload(Long id, HttpServletRequest request, HttpServletResponse response) {  
      
            // Get your file stream from wherever.  
            String fullPath = "E:/" + id +".rmvb";  
            File downloadFile = new File(fullPath);  
      
            ServletContext context = request.getServletContext();  
      
            // get MIME type of the file  
            String mimeType = context.getMimeType(fullPath);  
            if (mimeType == null) {  
                // set to binary type if MIME mapping not found  
                mimeType = "application/octet-stream";  
                System.out.println("context getMimeType is null");  
            }  
            System.out.println("MIME type: " + mimeType);  
      
            // set content attributes for the response  
            response.setContentType(mimeType);  
            response.setContentLength((int) downloadFile.length());  
      
            // set headers for the response  
            String headerKey = "Content-Disposition";  
            String headerValue = String.format("attachment; filename=\"%s\"",  
                    downloadFile.getName());  
            response.setHeader(headerKey, headerValue);  
      
            // Copy the stream to the response's output stream.  
            try {  
                InputStream myStream = new FileInputStream(fullPath);  
                IOUtils.copy(myStream, response.getOutputStream());  
                response.flushBuffer();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

     

//文件下载相关代码
    @RequestMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "b60bcf72-219d-4e92-88de-ed6b0ad9b0e7-2018-04-23-14-09-14.xls";// 设置文件名,根据业务需要替换成要下载的文件名
        if (fileName != null) {
            //设置文件路径
            String realPath = "D:\\eclipsworksapce1\\upgrade\\src\\main\\webapp\\upload\\tbox\\456789\\";
            File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/octet-stream");// 
                response.setHeader("content-type", "application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

二:获取文件名参数时,进行参数设置

		String fileName = URLDecoder.decode(getFileName,"utf-8");

三:下载文件,设置存在中文名的文件名编码

response.setContentType("application/force-download");
//通过设置头信息给文件命名,也即是,在前端,文件流被接受完还原成原文件的时候会以你传递的文件名来命名
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", URLEncoder.encode(tempFile.getName(), "utf-8")));

重点是这一句

URLEncoder.encode(tempFile.getName(), "utf-8"))

Nginx转发下载静态资源设置文件中文名称编码格式

response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) ); 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue调用Spring Boot项目实现文件下载,可以按照以下步骤进行: 1. 在Spring Boot项目编写Controller方法,实现文件下载功能。例如: ```java @GetMapping("/downloadFile") public ResponseEntity<Resource> downloadFile() { Resource resource = new FileSystemResource("/path/to/file"); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.txt"); return ResponseEntity.ok() .headers(headers) .contentLength(resource.contentLength()) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } ``` 在此方法,`/path/to/file` 是要下载文件的路径,`file.txt` 是要下载文件名。 2. 在Vue编写调用该接口的代码。例如: ```javascript downloadFile() { axios({ url: '/downloadFile', method: 'GET', responseType: 'blob' }).then(response => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'file.txt'); document.body.appendChild(link); link.click(); }); } ``` 在此代码,`axios` 是一个用于发送 HTTP 请求的 JavaScript 库。通过调用 `/downloadFile` 接口,获取文件数据并创建一个 Blob 对象,然后将其转换为 URL 并创建一个 `<a>` 标签,设置 `download` 属性为要下载文件名,最后触发点击事件即可下载文件。 注意:需要在 Vue 的 `mounted` 钩子函数引入 axios 库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值