zoukankan      html  css  js  c++  java
  • SpringBoot下载Excel文件,解决文件损坏问题197

    @RequestMapping(value = "dowlondTemplateFile")
    public ResponseEntity<byte[]> getFile(HttpServletResponse response) throws IOException {
    String fileName = "合同历史模板.xlsx";
    ClassPathResource resource = new ClassPathResource("static" + File.separator + "templateFile" + File.separator + fileName);
    try {
    File file = resource.getFile();
    //下载文件路径
    if (file.exists()) {
    if (SecurityUtil.getOnlineUser() != null) {
    LogUtil.info(SecurityUtil.getOnlineUser().getId(), SecurityUtil.getOnlineUser().getName(),
    "下载文件", "成功",
    "下载文件,id", "");
    }
    HttpHeaders headers = new HttpHeaders();
    //下载显示的文件名,解决中文名称乱码问题
    String downloadFileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
    //通知浏览器以attachment(下载方式)打开图片
    headers.setContentDispositionFormData("attachment", downloadFileName);
    //application/octet-stream : 二进制流数据(最常见的文件下载)。
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<>(FileUtils.readFileToByteArray(file),
    headers, HttpStatus.CREATED);
    } else {
    throw new FileNotExistException();
    }
    } catch (IOException e) {
    return null;
    }
    }


    解决 pom中加入如下 这个插件可以避免xlsx文件在resource目录下被自动压缩,这样就可以正常下载,打开了
                  <plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<version>2.6</version>
    				<artifactId>maven-resources-plugin</artifactId>
    				<configuration>
    					<encoding>UTF-8</encoding>
    					<nonFilteredFileExtensions>
    						<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
    					</nonFilteredFileExtensions>
    				</configuration>
    			</plugin>
    

      

  • 相关阅读:
    前端常见算法面试题之
    前端常见算法面试题之
    解决Windows7 Update无法检查更新
    linux停止tomcat为什么要kill其掉进程 而不是直接shutdown.sh
    ASP.NET Core 1.0 使用 MySQL for EF Core 1.0 (.NET Core 1.0)
    ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)
    11款免费好用的源代码管理桌面应用【转】
    陈灯WGF双缓冲绘图框架
    关于面试别人那点事儿
    数据类型
  • 原文地址:https://www.cnblogs.com/Ai-Hen-Jiao-zhi/p/13921428.html
Copyright © 2011-2022 走看看