【发布时间】:2016-07-23 14:57:44
【问题描述】:
所以这是我的代码,它已经可以工作了:
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();
}
}
但是,我必须使用 JDK 1.6 来完成这项工作。
Files.copy 仅适用于 Java 1.7。
有什么建议吗?
【问题讨论】:
-
顺便说一句,response.flushBuffer() 是完全没有必要的。