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

Posted godpo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件下载(download) 前后台备注自用相关的知识,希望对你有一定的参考价值。

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(‘ ‘);
}
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) 前后台备注自用的主要内容,如果未能解决你的问题,请参考以下文章

layui上传写法(自用)

基于Bottle 封装一下自用web前后端分离开发框架(1):项目规划

Xdebug 备注

用java给html文件添加必要的控制html代码片

关于dede后台登陆后一片空白

labeltools(自用)