0

This is the code i am following I dont know correctly where i need to change?

public class ImageServlet extends HttpServlet {

private static final int DEFAULT_BUFFER_SIZE = 10240;   
private String imagePath; 

public void init() throws ServletException {        
    String path =   System.getProperties().getProperty("jboss.server.base.dir");              
    this.imagePath = path + "/images";
    LogMsg.info("Upload Image path " + imagePath);     
}

protected void doGet(HttpServletRequest request, HttpServletResponse 
 response)throws ServletException, IOException
{

    String requestedImage = request.getPathInfo();        
    if (requestedImage == null) { 

        response.sendError(HttpServletResponse.SC_NOT_FOUND);            
       return;
    }

    file object.File image = new File(imagePath, 
     URLDecoder.decode(requestedImage, "UTF-8"));

    if (!image.exists()) {

        ignoreit.response.sendError(HttpServletResponse.SC_NOT_FOUND); 
        return;
    } 

       String contentType =getServletContext().getMimeType(image.getName());

    if (contentType == null || !contentType.startsWith("image")) {

        response.sendError(HttpServletResponse.SC_NOT_FOUND);            
        return;

  }       
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(contentType);
    response.setHeader("Content-Length",

    String.valueOf(image.length()));

     response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

  try {

        input = new BufferedInputStream(new FileInputStream(image),DEFAULT_BUFFER_SIZE);

        output = new BufferedOutputStream(response.getOutputStream(),DEFAULT_BUFFER_SIZE);   

        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);

    }

    } finally {            
        close(output);
        close(input);

    }
  }

   private static void close(Closeable resource) {     
      if (resource != null) {

        try {
            resource.close();

        } catch (IOException e) {               
            e.printStackTrace();
        }

    }

   }

  }
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Madan Mohan
  • 47
  • 1
  • 9

0 Answers0