二进制流下载zip或单个文件(spring + angular Js)

本文介绍了如何结合Spring后端和AngularJS前端,实现二进制流方式下载ZIP文件或单个文件。通过示例代码,详细讲解了后端的控制器处理文件流和前端调用接口进行下载的实现过程。
摘要由CSDN通过智能技术生成

1、后端代码:

public class DownloadActor extends AbstractSupportActor {
private Configuration configuration;
public LeaveQuestionDownloadActor() {
    configuration = new Configuration();
    configuration.setDefaultEncoding("UTF-8");
    // FTL文件所存在的位置
    configuration.setClassForTemplateLoading(this.getClass(), "");
}
public void Execute(Message message) throws Exception {
    // 查询下载文件所需数据
            String filename = 文件名 + ".doc";
            // 查询文件存储路径
            String downpath = this.getSqlSession().selectOne("XXX.XXX");
            // 判断该文件时候已经存在
            File outFile = new File(downpath + "/" + filename);
            if (outFile.exists()) {
                outFile.delete();
            }
            // 判断路径是否存在
            folder = new File(downpath);
            // 如果不存在,创建目录
            if (!folder.exists()) {
                folder.mkdirs();
            }
            try {
                Template t = configuration.getTemplate("Questionnaire.ftl");
                Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
                t.process(map, writer);
                writer.close();
            } catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值