InputStream输入流加载资源文件的三种方式
InputStream inputStream1 = this.getClass().getClassLoader().getResourceAsStream("helloWorld.zip");
InputStream inputStream2 = this.getClass().getResourceAsStream("helloWorld.zip");
InputStream inputStream3 = this.getClass().getResourceAsStream("/helloWorld.zip");
Spring下载resources下文件的方式
public void download(HttpServletRequest request, HttpServletResponse response) {
String fileName = "helloworld.zip";
ClassPathResource classPathResource = new ClassPathResource("helloworld.zip");
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024];
InputStream is = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {
is = classPathResource.getInputStream();
bis = new BufferedInputStream(is);
os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) os.close();
if (bis != null) bis.close();
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}