一、如何控制下载的文件的类型

只要给文件名命名时,后缀为txt的话,下载的就是txt文件,后缀为doc的话,下载的是Word文件。

示例:

controller

/**
     * 公钥下载
     * @param id
     * @param response
     * @return
     */
    @PostMapping("/uploadPublikey")
    public void uploadPublikey (Long id,HttpServletResponse response)throws Exception {
        equipmentService.uploadPublikey(id,response);
    }

service

@Override
    public void uploadPublikey(Long id, HttpServletResponse response) throws Exception {
        Equipment equipment = equipmentDao.selectByPrimaryKey(id);
        String enterpriseName = enterpriseDao.getName(equipment.getEntityId());
        String fileName = enterpriseName + "_" + equipment.getEquipmentNo() + ".doc";
        String content = equipment.getProdPubKey();
        FileUtil.downloadFile2(fileName, content, response);
    }

downloadFile2方法:

/**
     * 通过流下载文件
     *
     * @param fileName 文件名
     * @param response
     * @throws IOException
     */
    public static void downloadFile2(String fileName, String content, HttpServletResponse response) throws IOException {
        response = setResponse(fileName, response);
        OutputStream outputStream = response.getOutputStream();
        BufferedOutputStream buffer = new BufferedOutputStream(outputStream);
        buffer.write(content.getBytes());
        buffer.flush();
        buffer.close();
        outputStream.close();
    }

setResponse方法

/**
     * 设置响应信息
     * @param fileName
     * @param response
     * @throws UnsupportedEncodingException
     */
    private static HttpServletResponse setResponse(String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        response.setHeader("Access-Control-Expose-Headers", "content-disposition");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        return response;
    }

二、如何将文件名返回给前端

将文件名设置在Content-disposition(内容设置)的请求头中,返回给前端

Content-disposition:服务器告诉客户端以什么格式打开响应体数据,

属性值:

1)、in-line:默认值,在当前页面内打开。

2)、attachment;fifilename=xxx:以附件形式打开响应体。文件下载

如:

 

 另外:Content-Type:服务器告诉客户端本次响应体数据格式以及编码格式

 三、前端如何获取后台返回的文件名

//从请求头获取文件名
export function getFileNameFromHeader(response) {
  const disposition = response.headers["content-disposition"]
  let fileName = disposition.substring(disposition.indexOf("filename=") + 9);
  if (window.navigator.userAgent.indexOf("Firefox") > -1) {
    fileName = fileName.substring(fileName.indexOf("-8?B?") + 5, fileName.indexOf("?="))
    fileName = base64Util.decode(fileName);
  } else {
    fileName = decodeURIComponent(fileName);
  }
  return fileName
}

获取文件名后下载流文件

downLoad(row) {
      uploadPublikey({id: row.id}).then(res => {
       const fileName = getFileNameFromHeader(res)
       fileDownload(res.data,fileName)
      }).catch(err => {
        console.log(err)
      })
    },

 

posted on 2021-08-30 15:28  周文豪  阅读(1748)  评论(0编辑  收藏  举报