Local single file download

Swagger test Chinese will appear garbled, copy the path and open a new connection may not be garbled

first way

  /**
     * 下载
     */
    @GetMapping(value = "/download")
    @ApiOperation(value = "下载(管理端也可用)")
    public void download(String name,String path,HttpServletRequest request, HttpServletResponse response) {
    
    
        try {
    
    
            // path是指想要下载的文件的路径
            File file = new File("D:/image/"+path);
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
           /* if (request.getHeader("User-Agent").toUpperCase().contains("MSIE") ||
                    request.getHeader("User-Agent").toUpperCase().contains("TRIDENT")
                    || request.getHeader("User-Agent").toUpperCase().contains("EDGE")) {
                name = java.net.URLEncoder.encode(name, "UTF-8");
            } else {
                //非IE浏览器的处理:
                name = new String(name.getBytes("UTF-8"), "ISO-8859-1");
            }*/
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(name, "UTF-8"))) + '.' + ext);
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
    
    
            ex.printStackTrace();
        }
    }

Second way

 //downloadName 是下载后文件名称用来中文处理
 //pdfCreate+waterName 是下载的路径
            if (request.getHeader("User-Agent").toUpperCase().contains("MSIE") ||
                    request.getHeader("User-Agent").toUpperCase().contains("TRIDENT")
                    || request.getHeader("User-Agent").toUpperCase().contains("EDGE")) {
    
    
                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
            } else {
    
    
                //非IE浏览器的处理:
                downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + downloadName + "\"");
            FileInputStream fileInputStream = null;
            fileInputStream = new FileInputStream(pdfCreate+waterName);
            IoUtil.copy(fileInputStream , response.getOutputStream());
            fileInputStream.close();

Guess you like

Origin blog.csdn.net/whatevery/article/details/121073721