赞
踩
在maven编译时excel模板表就已经被损坏了,下载一个已经被损坏的Excel表肯定是无法正常打开的,数据乱码错误
为啥Excel表在编译后会被损坏,这里就涉及到maven resource
标签的使用。
pom.xml
添加
将excel格式文件排除,避免造成编译导致excel文件损坏
<build> <finalName>sim-api</finalName> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.xls</exclude> <exclude>**/*.xlsx</exclude> </excludes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xls</include> <include>**/*.xlsx</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.gaolei.sim.Application</mainClass> <layout>JAR</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
@RequestMapping(value = "/download", method=RequestMethod.POST, produces = { "application/json;" }) @ApiOperation(value = "下载风险研判信息记录模板", httpMethod = "POST", notes = "下载风险研判信息记录模板") public void download(HttpServletResponse res, HttpServletRequest req) throws Exception{ String fileName ="设备导出.xls"; String filePath = getClass().getResource("/static/" + fileName).getPath(); filePath = URLDecoder.decode(filePath, "UTF-8"); OutputStream os = res.getOutputStream(); InputStream in =new FileInputStream(filePath) ; //InputStream in = getClass().getClassLoader().getResourceAsStream("/static/"+fileName); res.setHeader("Content-Length", String.valueOf(in.available())); res.setContentType("multipart/form-data"); res.addHeader("Content-Disposition","attachment;fileName=" + new String(fileName.getBytes("GBK"),"UTF-8")); IOUtils.copy(in,os); os.flush(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。