vue axios 实现 文件流下载(前后端分离跨域问题的解决)

前端代码

vue axios 实现 文件流下载(前后端分离跨域问题的解决)_第1张图片

 后端以流的形式返回

单个文件下载

    @RequestMapping(value = "download", method = RequestMethod.POST)
    @ApiOperation(value = "单个文件下载")
    public HttpServletResponse download(@RequestBody FileDto fileDto, HttpServletResponse response) {
        InputStream fis = null;
        OutputStream toClient = null;
        try {
            // path是指欲下载的文件的路径。
            String dirPath = cloud_root + fileDto.getPath();
            // 取得文件名。
            String filename = fileDto.getFileName();
            File file = new File(dirPath + "/" + filename);
            fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();


            // 清空response
            response.reset();
            // 解决跨域问题,这句话是关键,对任意的域都可以,如果需要安全,可以设置成安前的域名
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            // 设置response的Header
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String(filename.getBytes(),"iso-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");

            toClient.write(buffer);
            toClient.flush();
            toClient.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }finally {
            try { // close input stream
                if (fis != null) {
                    fis.close();
                }
            } catch(Exception ex) {
            }
            try { // close output stream
                if (toClient != null) {
                    toClient.close();
                }
            } catch(Exception ex) {
            }
        }
        return response;
    }

 

批量文件下载(分目录)

@RequestMapping(value = "downloadZip", method = RequestMethod.POST)
    @ApiOperation(value = "批量文件下载")
    public void downloadZip(@RequestBody List fileDtos, HttpServletResponse response){
        String fileName = "download.zip";

        InputStream is = null;
        ZipOutputStream os = null;

        try {
            response.reset();
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));//设置下载的文件名称

            os = new ZipOutputStream(response.getOutputStream());

            byte[] buffer = new byte[1024*10];

            for (FileDto fileDto : fileDtos) {
                String downloadPath = cloud_root + fileDto.getPath();
                File file = new File(downloadPath + "/" +fileDto.getFileName());
                if (!file.exists() || !file.isFile()) {
                    continue;
                }
                is = new FileInputStream(file);

                os.putNextEntry(new ZipEntry(fileDto.getPath() + "/" + file.getName()));

                int num = is.read(buffer);
                while (num > -1) {
                    os.write(buffer, 0, num);
                    num = is.read(buffer);
                }

                os.closeEntry();

                is.close();
            }

            os.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            try { // close input stream
                if (is != null) {
                    is.close();
                }
            }
            catch(Exception ex) {
            }
            try { // close output stream
                if (os != null) {
                    os.close();
                }
            }
            catch(Exception ex) {
            }
        }
    }

批量下载这里有坑,另外一个项目批量下载的时候,没有目录层级,下载文件名相同的时候会报异常。

java.util.zip.ZipException: duplicate entry: ZP_3100192130_21585780_20191017114406.jpg
	at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:163)

 

你可能感兴趣的:(project)