zoukankan      html  css  js  c++  java
  • springboot文件下载案例

    service层下载文件的方法:

    1、加入依赖

    <!--文件流处理工具包-->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.2</version>
    </dependency>

    2、在springboot的application.properties文件中配置文件路径

    # 文件存储路径
    file.path=C:/image/

    3、编写下载代码

      @Value("${file.path}")  //注入
      private String filePath;

      @Override
    public void download(String fileId, HttpServletResponse response) { SysFile sysFile=sysFileMapper.selectByPrimaryKey(fileId); ServletOutputStream fileOutputStream = null; response.setContentType("multipart/form-data"); try { //万能乱码问题解决 String fileName = new String(sysFile.getFileName().getBytes("UTF-8"), "ISO-8859-1"); // 设置文件下载响应头 response.setHeader("Content-disposition", String.format("attachment;filename=%s",fileName)); } catch (Exception ex) { log.error("e:{}",ex); throw new BusinessException(BaseResponseCode.OPERATION_ERROR); } try { File file=FileUtils.getFile(filePath,sysFile.getFileName()); fileOutputStream = response.getOutputStream();    IOUtils.write(FileUtils.readFileToByteArray(file),fileOutputStream); } catch (IOException e) { log.error("e:{}",e); throw new BusinessException(BaseResponseCode.OPERATION_ERROR); }finally { try { if(fileOutputStream != null){ fileOutputStream.close(); } } catch (IOException e) { log.error("e:{}",e); throw new BusinessException(BaseResponseCode.OPERATION_ERROR); } } }
  • 相关阅读:
    理解闭包Closure
    理解商集
    理解格
    理解距(数学)
    微积分英文词汇,高数名词中英文对照,高等数学术语英语翻译一览
    对Extjs中store的多种操作
    mysql中的除法取整
    【python】用asq实现count(distinct cln)
    Timer 和TimerTask的使用
    使用vim.rc配置vim
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690635.html
Copyright © 2011-2022 走看看