基本上只需要创建一个
Servlet它获取的
InputStream它与帮助
FileInputStream,并将其写入到
OutputStream的
HttpServletResponse一个正确设定的响应头与至少一起
content-type。最后
src,在
<img>元素的属性中将此Servlet 以及文件标识符作为请求参数或pathinfo进行调用。例如:
File file = new File("c:/abc.jpg");response.setContentType(getServletContext().getMimeType(file.getName()));response.setHeader("Content-Length", String.valueOf(file.length()));response.setHeader("Content-Disposition", "inline; filename="" + file.getName() + """);BufferedInputStream input = null;BufferedOutputStream output = null;try { input = new BufferedInputStream(new FileInputStream(file)); output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[10240]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); }} finally { if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}}


