码迷,mamicode.com
首页 > 其他好文 > 详细

文件下载(download) 前后台备注自用

时间:2019-11-25 13:41:34      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:shm   request   href   agg   har   fail   cep   const   work   

1、后台

      1.1 ResponseBean 返回封装的格式

@Data
@Accessors(chain = true)
public class ResponseBean {
    private String retCode;
    private String retMsg;
    private Map<Object, Object> retMap;

}

  1.2请求封装的格式

@Data
@Accessors(chain = true)
public class RequestBean {
    private Map sysMap;
    private List<Object> parameterList;

}

  1.3后台服务接口

package com.test.demo.controller;

import com.test.demo.common.RequestBean;
import com.test.demo.common.ResponseBean;
import com.test.demo.common.RetCode;
import com.test.demo.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @description: 文件相关
* @author: ZhuGaoPo
* @version:1.0
*/
@Slf4j
@RequestMapping(value = "/file")
@RestController
public class FileController {

@PostMapping(value = "/downloadFile")
@ApiOperation("文件下载到本地")
public ResponseBean downloadFile(@RequestBody RequestBean requestBean) {
log.info("接收到的数据为:" + requestBean);
ResponseBean response = new ResponseBean();
String filePath = String.valueOf(requestBean.getParameterList().get(0));
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GBK"));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
stringBuilder.append(line).append(‘\n‘);
}
response.setRetCode(RetCode.SUCCESS);
Map<Object, Object> retMap = new HashMap<>(RetCode.MAP_SIZE);
retMap.put("data", stringBuilder.toString());
response.setRetMap(retMap);
} catch (Exception e) {
response.setRetCode(RetCode.FAIL);
log.error("下载模板失败", e);
} finally {
try {
Objects.requireNonNull(br).close();
} catch (IOException e) {
e.printStackTrace();
}
}
log.info("返回的数据为:" + response);
return response;
}
}

  2、前台

             <el-button type="warning" plain @click="downloadFile()">
               下载
              </el-button>

  

 downloadFile() {
//filePath为文件服务的路径
      const data = {
        sysMap: {
          features: ‘下载模板‘
        },
        parameterList: [this.filePath]
      }
      this.$request({
        url: ‘/file/downloadFile‘,
        method: ‘post‘,
        data
      }).then(res => {
        if (res.retCode === ‘IF0000‘) {
          // 成功、加上 ‘\ufeff’ 解决导出 csv 乱码问题
          const blob = new Blob([‘\ufeff‘ + res.retMap.data], { type: ‘application/json;charset=UTF-8‘ })
          const a = document.createElement(‘a‘)
          // 指定下载的文件名
          a.download = ‘黑名单模板.csv‘
          // URL对象
          a.href = URL.createObjectURL(blob)
          // 模拟点击
          a.click()
        } else {
          return false
        }
      }).catch(e => {})
    },

  

文件下载(download) 前后台备注自用

标签:shm   request   href   agg   har   fail   cep   const   work   

原文地址:https://www.cnblogs.com/godpo/p/11926914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!