我正在尝试制作一个可以通过网址识别的图像主机。 对于GET方法,我这样做:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("id");
String path = request.getServletContext().getRealPath("/uploaded");
File folder=new File(path);
File file = new File(folder,filename);
if (!file.exists())
throw new ServletException("file not found");
response.setContentLength((int)file.length());
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
如果我在我的开发环境(Netbeans + Glassifh)中尝试顺利进行,那么当我在Amazon Web Services中部署它时,响应是这样的:�bc"IdIX�P�F��IJ/)�
答案 0 :(得分:1)
尝试设置适当的Content-Type
响应
e.g。对于JPEG图像:
response.setContentType("image/jpeg");