概述
JAVA实现resources下的文件,本次实现的是下载一个模板文件。导入的包都是默认的io流这些。
首先在pom.xml引入包
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
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, List<String> titles) 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;
}
/**
* 下载excel模板
*/
public static void export(Workbook wb, String fileName, List<String> 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));
List<String> titleList = new ArrayList();
ExcelUtil.export(wb, ProjectConfiguration.fileName, titleList, response);
} catch (Exception e) {
e.printStackTrace();
}
}
测试(使用postman测试)
可以看见就实现下载了resources下面的文件了。