业务需求
导入excel,如果有报错用excel文件方式返回。错误的校验信息都封装在excel中
先生成临时文件存在本地,将文件名传递给前端。然后再传递文件名,获取文件后删除文件。
Java临时文件
File类中有两种方法可用于在java中创建临时文件。
createTempFile(String prefix, String suffix, File directory)
createTempFile(String prefix, String suffix)
有random做后缀
.getCanonicalPath()
获取路径从而获取文件名
String fullName = System.getProperty(“java.io.tmpdir”) + File.separator + fileName;
将文件给response文件流
File file = new File(filePath);
try {
InputStream stream = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream()
byte buff[] = new byte[1024];
int length = 0;
while ((length = stream.read(buff)) > 0) {
out.write(buff,0,length);
}
stream.close();
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}