前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >完整的 Spring Boot 下载文件示例代码

完整的 Spring Boot 下载文件示例代码

作者头像
一个会写诗的程序员
发布2019-02-26 13:05:01
2.3K0
发布2019-02-26 13:05:01
举报
文章被收录于专栏:一个会写诗的程序员的博客
代码语言:javascript
复制
package com.alibaba.alpha.download;

import com.alibaba.alpha.mapper.UiTestCaseMapper;
import com.alibaba.alpha.model.UiTestCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class DownloadController {
    @Autowired
    UiTestCaseMapper uiTestCaseMapper;

    /**
     * /downloadTestCase?testCaseId=${testCaseId}
     *
     * @param testCaseId
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadTestCase")
    public void downloadTestCase(Long testCaseId, HttpServletResponse response) throws Exception {
        // 1.用例数据
        UiTestCase UiTestCase = uiTestCaseMapper.selectByPrimaryKey(testCaseId);
        String tcName = UiTestCase.getName();
        // 2.文件存放路径
        String tmpDir = System.getProperty("user.home") + "/sidejson/";
        File tmpDirFile = new File(tmpDir);
        if (!tmpDirFile.exists()) {
            tmpDirFile.mkdir();
        }
        // 3.sideJson 文件
        String fileName = tmpDir + tcName;
        File sideJsonFile = new File(fileName);
        if (!sideJsonFile.exists()) {
            sideJsonFile.createNewFile();
        }
        FileWriter fileWritter = new FileWriter(fileName, true);
        fileWritter.write(UiTestCase.getSideJson());
        fileWritter.close();

        // 配置文件下载
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        // 下载文件能正常显示中文, 可以导入 iRecorder Web IDE中的 .side 文件
        String filename = URLEncoder.encode(tcName + ".side", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        // 实现文件下载
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(sideJsonFile);
            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("Download successfully! " + fileName);
        } catch (Exception e) {
            System.out.println("Download failed! " + fileName);
            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();
                }
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.01.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档