Java:excel模板文件打包后乱码问题

问题:项目中resources目录下的excel模板打包后文件乱码了
原因:maven打包会对资源统一编码
Java:excel模板文件打包后乱码问题_第1张图片
解决方式:忽略maven打包时需要编码的文件

在pom.xml加入下面代码

  
        org.apache.maven.plugins
        maven-resources-plugin
        2.7
        
          UTF-8
          
            xls
            xlsx
            dat
          
        
      

模板下载前端代码AngularJS:

$scope.downTemp = function () {
        $http({
            method: 'post',
            url: 'load/downloadTemp',
            responseType: 'arraybuffer',
        }).success(function (data) {
            var fileName = "导入模板.xls";
            var url = window.URL.createObjectURL(new Blob([data],{type:"application/vnd.ms-excel"}));
            var link = document.createElement('a');
            link.style.display = 'none';
            link.href = url;
            link.setAttribute('download',fileName);
            document.body.appendChild(link);
            link.click();
        })
    }

后端:

 @RequestMapping("/downloadTemp")
    public void downloadTemp(HttpServletResponse response) throws UnsupportedEncodingException {
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;fileName=导入模板.xls");
        try {
            OutputStream out = response.getOutputStream();
            Resource resource = new ClassPathResource("templates/导入模板.xls");
            InputStream in = new FileInputStream(resource.getFile());
            IOUtils.copy(in,out);
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Java)