Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 22nd, 2004, 07:41 AM
Authorized User
Join Date: Sep 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with images

I have an application where i allow users to upload images as blob data type to the database...

How do i restrict the size of the image (both pixel size n image size) that is being uploaded by the user...

Preferably i would like the user to be able to upload an image of pixel size(say 100 x 100) or 50Kb ..

And while displaying the image(on a jsp page) i wud lyk to display it as an image of pixel size (say 50 x 50)..

I guess it wud be wiser to store the image(as blob data type) after resizing it to the required display size ie.50 x 50..

Would be grateful if anybody has a solution..

Old September 22nd, 2004, 11:45 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr

Refer to earlier post at :

Gokulan Ethiraj
Old September 23rd, 2004, 12:15 AM
Authorized User
Join Date: Sep 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts

Thnx 4 replyin gokul_blr...

I suppose u misunderstood my requirement..

The post that u r referring 2, deals with displaying images in a JSP page..

Whereas i want 2 restrict the size(both pixel size n byte size) of the image that is being uploaded by the users of my application..

Much like the same way that sites lyk www.distant-help.com restrict the image size of the Avatar tat u upload...

The avatar feature is available in the "user cp(control panel)" section of the afore-mentioned forum..

Hope u understand my requirement now...
Old September 24th, 2004, 06:23 AM
Authorized User
Join Date: Sep 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts

I finally found the solution at the following site:

Old September 24th, 2004, 12:25 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts

I found a solution of sorts.
I upload the file to a byte array, restricting the size of this causes an exception if the file is too big.

Its a bad fix as it does not fail until the file has exceeded the limit, meaning you have already wasted the bandwidth.

I am not sure if the post method gives the file size, its a problem I will re visit when I get time.

Old September 24th, 2004, 12:35 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts

Sorry forgot to mention, I found a solution to resize images on the sun java forum, will post code later, also found code to add a watermark if your interested:


Old September 24th, 2004, 12:51 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts

Hello again,

Heres the code Ive got, Im afraid Ive messed with it a lot, Ive left the commented stuff in just in case you want to play with it.
Sorry its such a mess, cant remember where I got it, think it was on a sun turorial, you will need to get the relevant API's if you do not have them installed.

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;

public class ResizeImageServlet

    public final void doPost(String inScale )throws Exception
          int targetWidth=0;
          int targetHeight=0;

          // Get a path to the image to resize.
          // ImageIcon is a kluge to make sure the image is fully
          // loaded before we proceed.
          Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().

          // Calculate the target width and height
          float scale = Float.parseFloat(inScale)/100;
          targetWidth = (int)(sourceImage.getWidth(null)*scale);
          targetHeight = (int)(sourceImage.getHeight(null)*scale);

          BufferedImage resizedImage = this.scaleImage

          // Output the finished image straight to the response as a JPEG!
          //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder

          //InputStream is = new BufferedImag("test");
          //JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);

          ByteArrayOutputStream out = new ByteArrayOutputStream();
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          byte[] b = out.toByteArray();
          //int offset=0;
          //int length = (b, 0, b.length());
          //inputLine = new String (b, 0, 0, length);


          JPEGCodec.createJPEGEncoder(new FileOutputStream("C:\\testImage.jpg")).encode(resi zedImage);

          for (int i=0; i<200; i++)

          //InputStream is = new FileInputStream(img);


          String databaseDriver = "org.gjt.mm.mysql.Driver";
          Class.forName( databaseDriver );
          String databaseName = "jdbc:mysql://localhost/test";
          Connection con = null;
          con = DriverManager.getConnection(databaseName, "testDb","");

          String str = "INSERT INTO images (detail, image) VALUES (?,?)";
          PreparedStatement pstmt = con.prepareStatement(str);

          pstmt.setString(1, "test Sting");

          //pstmt.setBinaryStream( 2, resizedImage, (int)(resizedImage.length()));
          pstmt.setString( 2, "resizedImage");

          pstmt = null;
          con = null;

        catch(Exception e)


    private BufferedImage scaleImage(Image sourceImage, int width, int height)
        ImageFilter filter = new ReplicateScaleFilter(width,height);
        ImageProducer producer = new FilteredImageSource
        Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer);

        return this.toBufferedImage(resizedImage);

    private BufferedImage toBufferedImage(Image image)
        image = new ImageIcon(image).getImage();
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)
        ,image.getHeight(null),BufferedImage.TYPE_INT_RGB) ;
        Graphics g = bufferedImage.createGraphics();
        g.fillRect(0,0,image.getWidth(null),image.getHeigh t(null));

        return bufferedImage;

Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
Working with TIFF images Freedolen Perl 3 March 16th, 2007 10:55 AM
Php code to put&get images from mysql not working! angelbeans PHP Databases 1 September 8th, 2006 12:44 PM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
Working with Images Issue Capt D PHP Databases 6 January 31st, 2005 04:49 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.