vue+Springboot下载文件

前端代码
<el-button type="primary" @click="dowload2('1662023440868上传测试.jpg')">下载</el-button>

  js代码

 dowload2(msg){
          let href = "http://localhost:9090/myFiles/downloadFile?flag="+msg;
          let downloadElement = document.createElement('a')
          downloadElement.style.display = 'none'
          downloadElement.href = href
          document.body.appendChild(downloadElement)
          // 点击下载
          downloadElement.click()
      },

 后端代码 方法1

 @RequestMapping("/downloadFile")
    public void downloadFiles(@RequestParam("flag") String downUrl, HttpServletRequest request, HttpServletResponse response) {
        downUrl = "D:/FileUpload/"+downUrl;
        System.out.println("downUrl========"+downUrl);
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        byte[] bytes = new byte[1024];
        File file = new File(downUrl);
        String fileName = file.getName();
        System.out.println("本次下载的文件为" + downUrl);
        // 获取输出流
        try {
            // StandardCharsets.ISO_8859_1 *=UTF-8'
            // response.setHeader("Content-Disposition", "attachment;filename=" +  new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 以流的形式返回文件
            response.setContentType("application/octet-stream;charset=utf-8");
            inputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();
            int i = bufferedInputStream.read(bytes);
            while (i != -1) {
                outputStream.write(bytes, 0, i);
                i = bufferedInputStream.read(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
      }
    

  方法2

@RequestMapping("/getFile")
    public void getFile(@RequestParam("flag") String flag, HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println("getFile下载参数flag======"+flag);
        Result res = new Result();
        String path = "";
        //response.setHeader("Content-Disposition", "attachment;filename=" +  URLEncoder.encode(flag, "UTF-8"));
        // 以流的形式返回文件
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(flag, "UTF-8"));
        try{
            System.out.println("下载文件flag====="+flag);
            OutputStream os;
            path = "D:/FileUpload/"+flag;
            byte[] bytes =  FileUtil.readBytes(new File(path));
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            os.close();
            System.out.println("path====="+path);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

  pom.xml文件

<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.5.1</version>
		</dependency>

		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.8.5</version>
		</dependency>

  

 

posted @ 2022-09-02 13:02  红尘沙漏  阅读(1021)  评论(0编辑  收藏  举报