File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes loading image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "loading image" Watch "loading image" New topic
Author

loading image

Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
import java.awt.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Acme.JPM.Encoders.GifEncoder;
public class Confidentializer extends HttpServlet {
Frame frame = null;
Graphics g = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Construct a reusable unshown frame
frame = new Frame();
frame.addNotify();
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
try {
// Get the image location from the path info
String source = req.getPathTranslated();
if (source == null) {
throw new ServletException("Extra path information " +
"must point to an image");
}
// Load the image (from bytes to an Image object)
MediaTracker mt = new MediaTracker(frame);
// frame acts as an ImageObserver
Image image = Toolkit.getDefaultToolkit().getImage(source);
mt.addImage(image, 0);
try {
mt.waitForAll();
}
catch (InterruptedException e) {
getServletContext().log(e, "Interrupted while loading image");
throw new ServletException(e.getMessage());
}
// Construct a matching-size off screen graphics context
int w = image.getWidth(frame);
int h = image.getHeight(frame);
Image offscreen = frame.createImage(w, h);
g = offscreen.getGraphics();
// Draw the image to the off screen graphics context
g.drawImage(image, 0, 0, frame);
// Encode the off screen graphics into a GIF and send it to
// the client
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(offscreen, out);
encoder.encode();
}
finally {
// Clean up resources
if (g != null) g.dispose();
}
}
public void destroy() {
// Clean up resources
if (frame != null) frame.removeNotify();
}
}
Hi, I tried to invoke the above servlet with a gif file but the browser shows blank. Can someone please help me explain what the problem might be?
Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
Try setting the content length - some browsers are very picky about this. You will have to output to a ByteArrayOutputStream first in order to get that length. That will give you an additional chance to check that everything is working since you can also write that byte array to a file.
Bill

------------------
author of:


Java Resources at www.wbrogden.com
Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
Thank you so much Bill!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loading image
 
Similar Threads
Image
how image path should be passed to servlet
drawImage not getting the image drawn before printing!
Need help with my game.
how to use drawImage in an object