0

I am trying to use progressive image to load on site.I am a beginner and have no idea how to get a progressive image!

Basically can we convert a normal uploaded image to progressive image using java? If yes, please provide some idea or any references.Main objective is to render large image.

Thank you

Here is how my servlet looks

package com.throttle.images;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
  * Servlet implementation class ImagingServlet
*/
public class ImagingServlet extends HttpServlet{
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ImagingServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("image/jpg");  

        ServletOutputStream out;  
        out = response.getOutputStream(); 
        String name = request.getParameter("name");
        FileInputStream fin = new FileInputStream("system file path"+name);  
        System.out.println(name);
        BufferedInputStream bin = new BufferedInputStream(fin);  
        BufferedOutputStream bout = new BufferedOutputStream(out);  
            int ch =0; ;  
            while((ch=bin.read())!=-1)  
            {  
            bout.write(ch);
            bout.flush();
            //out.flush();
            //delay();
            //response.flushBuffer();
            }  
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}


void delay(){
    for(int i=0;i<1000;i++){
        for(int j=0;j<1000;j++){
            ;;  
        }

    }
}

}

vikrant
  • 399
  • 3
  • 12
  • 1
    To test, ensure the image is not cached: `"... .jpg?x=" + System.currentTimeMillis()`. And for testing maybe make your own delivery servlet that slows down the download of the image: maybe a flush after every 16 bytes. – Joop Eggen Dec 03 '14 at 12:58
  • Thanks for reply.I will make sure its not cached.Can u give more idea regarding delivery servlet that slows down download of image?Because this is what i am looking for.. – vikrant Dec 04 '14 at 05:43
  • 1
    The technical term for intentional slow-down is **throtling**, general as servlet filter to enable massive downloads without DoS vulnerability. It probably is easiest to start writing the servlet yourself: image path as param, `getRealPath("/images/x.jpg")` FileInputStream, every umpteenth byte a `flush()`, maybe even a `Thread.sleep(500)`. – Joop Eggen Dec 04 '14 at 10:26
  • 1
    Thanks a lot.I was successfully able to create delivery servlet for a single image.Now implementing for multiple images using threads – vikrant Dec 08 '14 at 06:06
  • But i wonder how do i handle multiple requests to display multiple set of images? – vikrant Dec 08 '14 at 10:25
  • The servlet will get called several times, even parallel. So a servlet should best use only local variables, fields are probably bugs. Maybe you mean using a common counter (AtomicInteger?) for adaptive throttling and such. Needs to be error prone, like a counter for a car park deck / parking lot. There are many optimizations, like applying the ALOHA sat system: time slots for reading the same file only once, and then delivering them to several requests. In your case I would wait; maybe check out a load test tool. – Joop Eggen Dec 08 '14 at 10:56
  • P.S. I take it the image path is an URL parameter resp. part of the URL like in "/takeiteasy/x.jpg" - URL mapping "/takeiteasy/*" – Joop Eggen Dec 08 '14 at 11:08
  • presently this how i am requesting – vikrant Dec 08 '14 at 12:53
  • Then that should work for many images. – Joop Eggen Dec 08 '14 at 13:32
  • It works great.awesome!..But it is missing smoothness in the display.Browser hinders other user interaction until images are loaded. – vikrant Dec 09 '14 at 06:16

1 Answers1

2

Images already render progressively in a web browser -- but you can do even better. Simply save your GIF or PNG images with the "interlaced" option, or your JPEG images with the "progressive" option

references here

David Clifte
  • 359
  • 4
  • 12