-2

I want the JSP program where user wants to upload an image into MySQL DB while uploading I want to convert image Dimensions as per my specified size like 300x300px. Plz help me.

my JSP code

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <form method="post" action="imageAPI" enctype="multipart/form-data">
            <input type="file" name="pic"/><br/>
            <input type="submit" value="send"/>
        </form>
    </body>
</html>

my servlet code

@WebServlet(urlPatterns = {"/imageAPI"})
@MultipartConfig(maxFileSize = 16177215)
public class imageAPI extends HttpServlet {
private String dbURL = "jdbc:mysql://localhost/OBS";
private String dbUSER = "root";
private String dbPASS = "1234";

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Connection con = null;
    InputStream is = null;
    Image image = null;
    BufferedImage thumb = null;
    Part filePart = req.getPart("pic");
    int thumbWidth = 600;
    int thumbHeight = 600;
    if(filePart != null){

        is = filePart.getInputStream();
        image = ImageIO.read(is);
        thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumb.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE); 
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    ImageIO.write(thumb, "JPG", resp.getOutputStream());

}

    try{
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        con = DriverManager.getConnection(dbURL, dbUSER, dbPASS);

        String sql = "insert into imageapi (image) values(?)";
        PreparedStatement ps = con.prepareStatement(sql);
        if(is!= null){
            ps.setBlob(1,(Blob) ImageIO.read((ImageInputStream) thumb));

        }

        con.close();
        ps.close();
    }catch(SQLException ex){
        ex.printStackTrace();
    }

}

}

  • Please refer below, hope will help. http://stackoverflow.com/questions/2938698/jsp-how-to-scale-an-image – user6257780 Mar 11 '17 at 07:41
  • yes, its working and changing the size of the image too. but I want to store it in my DB. – Syed Zeeshan Ali Mar 11 '17 at 08:16
  • You are asking two (maybe three) different things, please ask questions focused on one thing (eg one for changing the size of the image, a second for storing an image in a database, maybe a third for changing the name ). – Mark Rotteveel Mar 11 '17 at 08:59
  • Okay Look leave changing name thing. one problem is solved the i.e changing size but after changing size I don't want to display image on browser I want to store it in the DB. and my image column is of blob type. Now tell me what should I do. Thak You in Advanced – Syed Zeeshan Ali Mar 11 '17 at 09:52

1 Answers1

-1

to store in DB you have to convert to bytes, then save as Blob

Blob blob = new SerialBlob(myByte );
  • Save as `bolb` or `blob` ? Try editing a bit your answer (uppercase when starting a sentence, code should be in code form) – Lhooq Mar 11 '17 at 08:54