1

I'm trying to make an Image host thath could be identified by an url. For GET Method I'he done like this:

 @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();
}

If I try it in my developement environment (Netbeans+Glassifh) everithing go smooth but when i deploy it in Amazon Web Services response is like this: �bc"IdIX�P�F��IJ/)�

Can you help me? Thank you!

Fucio
  • 475
  • 3
  • 11
  • Perhaps it has to do with the character encoding. Your local environment might use a different variant than the AWS default and since you haven't specified anything it might get messed up. But that's just a guess. – yarwest Jan 01 '18 at 14:46
  • 2
    Not to do with the error but set the ContentType of the response. – Diego C Nascimento Jan 01 '18 at 14:57

1 Answers1

1

Try setting an appropriate Content-Type of the response

e.g. for JPEG images:

response.setContentType("image/jpeg");
Ward
  • 2,799
  • 20
  • 26