Hi,
Following is a servlet, which is being invoked by a cgi-script at the
server. The servlet should pick up a jpeg image from the server's hard
disk, manipulate it and save it back as jpeg on the hard disk. Also, it
should send back the control to the cgi-script. Moreover, I must mention
that the servlet is NOT supposed to display anything at the client end.
It compiles without any problem. I am running it on Websphere. It give
the IlllegalArgumentException error, while creating the image.
Any help is appreciated.
TIA
Gaurav
//The code is from Jason Hunter's book. (Only diff. is in the Encoder
class)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.Toolkit;
import java.awt.*;
import JpegEncoder.*;
import Jpeg.*;
public class CodeImg extends HttpServlet {
Frame frame = null;
Graphics g = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
frame = new Frame();
frame.addNotify();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
try {
File infile = new File(
"c:\\WebSphere\\AppServer\\servlets\\tmp\\ben_out.jpg" );
String source = infile.getName();
if (source == null) {
throw new ServletException("Extra path information" + "must point to an
image");
}
MediaTracker mt = new MediaTracker(frame);
Image image = Toolkit.getDefaultToolkit().getImage(source);
mt.addImage(image, 0);
try {
mt.waitForAll();
}
catch (InterruptedException e) {
getServletContext().log("Interrupted while loading image");
throw new ServletException(e.getMessage());
}
int w = image.getWidth(frame);
int h = image.getHeight(frame);
Image offscreen = frame.createImage(w, h);
g = offscreen.getGraphics();
g.drawImage(image, 0, 0, frame);
g.setFont(new Font("Times", Font.BOLD | Font.ITALIC, 30));
g.drawString("CONFIDENTIAL", 10, 30);
File outfile = new
File("c:\\WebSphere\\AppServer\\servlets\\tmp\\ben_oout.jpg");
FileOutputStream fos = new FileOutputStream(outfile);
res.setContentType("image/jpeg");
JpegEncoder jpg = new JpegEncoder(image, 50, fos);
jpg.Compress();
}
finally {
if (g != null) g.dispose();
}
}
public void destroy() {
if (frame != null) frame.removeNotify();
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html