So this is the code I have, which already works:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws Exception {
String pathToFile = "myimage.jpg";
File file = new File(pathToFile);
response.setHeader("Content-Type", "image/jpeg");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
Files.copy(file.toPath(), response.getOutputStream());
response.flushBuffer();
}
}
However, I must make this work with JDK 1.6.
Files.copy is only available with Java 1.7.
Any suggestions?