springboot读取war包下图片并展示到浏览器

@RequestMapping("/photo")
public void photo(HttpServletResponse response) throws IOException {
String url = "/Users/amanda/Desktop/test.war";
String path = "demo.jpg";
String filePath = url;
JarFile jarFile = null;
try {
jarFile = new JarFile(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
Enumeration<JarEntry> entries = jarFile.entries();
InputStream inptStrm = null;
ServletOutputStream outputStream = null;
while (entries.hasMoreElements()) {
System.out.println(path);
JarEntry nextElement = entries.nextElement();
if(nextElement.getName().indexOf(path)>-1){
inptStrm = jarFile.getInputStream(nextElement);
response.setContentType("image/png");
outputStream = response.getOutputStream();

int len;
byte[] buffer = new byte[4096];
while ((len = inptStrm.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}
}
outputStream.close();
inptStrm.close();
}
原文地址:https://www.cnblogs.com/zhang-zhao/p/15155197.html