• 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); } } }
  • 相关阅读:
    spring boot的actuator
    mongo的用户角色配置
    spring boot使用AOP统一处理web请求
    (原)luarocks install 提示 failed fetching manifest
    (原)ubuntu中安装kate
    (原+转)Ubuntu16.04软件中心闪退及wifi消失
    (原)torch使用caffe时,提示CUDNN_STATUS_EXECUTION_FAILED
    (原)torch中微调某层参数
    (原)torch的apply函数
    (原)torch的训练过程
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690635.html
Copyright © 2020-2023  润新知