JAVA实现resources下的文件,本次实现的是下载一个模板文件。导入的包都是默认的io流这些。
首先在pom.xml引入包
org.apache.maven.plugins maven-resources-plugin2.6 xlsx xls docx
ExcelUtil工具类
import com.alibaba.nacos.common.utils.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.util.List; public class ExcelUtil { private static Workbook appendTitles(Workbook wb, Listtitles) throws Exception { Sheet sheet = wb.getSheetAt(0); // 设置表头的说明 Row topRow = sheet.getRow(1); for (int i = 0; i < titles.size(); i++) { Cell cell = topRow.createCell(i + 6); // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(titles.get(i)); sheet.setColumnWidth( i + 6, titles.get(i).getBytes().length * 258); } return wb; } public static void export(Workbook wb, String fileName, List titles, HttpServletResponse response) throws Exception { try { if(!CollectionUtils.isEmpty(titles)){ wb = appendTitles(wb, titles); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); baos.flush(); byte[] aa = baos.toByteArray(); response.addHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName); response.setHeader("Content-Type", "application/vnd.ms-excel"); response.setCharacterEncoding("UTF-8"); response.getOutputStream().write(aa); response.setContentLength(aa.length); } catch (Exception e) { e.printStackTrace(); throw new Exception("write excel to stream error!", e); } finally { if (response.getOutputStream() != null) { response.getOutputStream().flush(); } } } }
ProjectConfiguration(扫描resources下面的文件)
import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.IOException; import java.io.InputStream; @Component public class ProjectConfiguration { @Autowired private ResourceLoader resourceLoader; public static byte[] fileBytes; public static String fileName; @PostConstruct public void init(){ Resource resource = resourceLoader.getResource("classpath:templates/areaTemplate.xls"); try { InputStream inputStream = resource.getInputStream(); fileName = "areaTemplate.xls"; fileBytes = IOUtils.toByteArray(inputStream); } catch (IOException e) { e.printStackTrace(); } } }
Controlleer控制层调用
@CrossOrigin @PostMapping(value = "/downloadTemplate" , produces = {"application/json;charset=UTF-8"}) public WeaResult downloadTemplate(HttpServletResponse response){ byte[] fileSource = ProjectConfiguration.fileBytes; //防止对原数据进行 *** 作 byte[] fileBytes = Arrays.copyOf(fileSource, fileSource.length); //下载模板 try { Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(fileBytes)); ListtitleList = new ArrayList(); ExcelUtil.export(wb, ProjectConfiguration.fileName, titleList, response); } catch (Exception e) { e.printStackTrace(); } }
测试(使用postman测试)
可以看见就实现下载了resources下面的文件了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)