How to upload and save image in mysql using jsp?

i have this code.


<head><title>Image Upload</title></head>

  <form action="UploadImage" method="post" enctype="multipart/form-data"
           name="productForm" id="productForm"><br><br>
    <table width="400px" align="center" border=0 style="background-color:ffeeff;">
        <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
           Image Details</td>

        <td align="center" colspan=2>&nbsp;</td>

        <td>Image Link: </td>
          <input type="file" name="file" id="file">

        <td><input type="submit" name="Submit" value="Submit"></td>
        <td colspan="2">&nbsp;</td>



import java.sql.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class UploadImage extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("request: " + request);
        if (!isMultipart) {
            System.out.println("File Not Uploaded");
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;

            try {
                items = upload.parseRequest(request);
                System.out.println("items: " + items);
            } catch (FileUploadException e) {
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem);
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    System.out.println("name: " + name);
                    String value = item.getString();
                    System.out.println("value: " + value);
                } else {
                    try {
                        String itemName = item.getName();
                        Random generator = new Random();
                        int r = Math.abs(generator.nextInt());

                        String reg = "[.*]";
                        String replacingtext = "";
                        System.out.println("Text before replacing is:-" + itemName);
                        Pattern pattern = Pattern.compile(reg);
                        Matcher matcher = pattern.matcher(itemName);
                        StringBuffer buffer = new StringBuffer();

                        while (matcher.find()) {
                            matcher.appendReplacement(buffer, replacingtext);
                        int IndexOf = itemName.indexOf(".");
                        String domainName = itemName.substring(IndexOf);
                        System.out.println("domainName: " + domainName);

                        String finalimage = buffer.toString() + "_" + r + domainName;
                        System.out.println("Final Image===" + finalimage);

                        File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\\\" + finalimage);
                        System.out.println("<img src=images/" + finalimage + ">");

                        String userName = "root";
                        String password = "";
                        String url = "jdbc:mysql://localhost/thundercatz";
                        String driver = "com.mysql.jdbc.Driver";
                        // connection = DriverManager.getConnection(url, userName, password);

                        Connection connection = null;
                        Statement statement;
                        // String url = "jdbc:mysql://localhost/";
                        // String dbName = "thundercatz";
                        // String driver = "com.mysql.jdbc.Driver";
                        // String username = "root";
                        //  String userPassword = "";
                        String strQuery = null;
                        //  String strQuery1 = null;
                        //String imgLen="";

                        try {
                            System.out.println("itemName::::: " + itemName);
                            connection = DriverManager.getConnection(url, userName, password);
                            statement = connection.createStatement();
                            strQuery = "INSERT INTO testimage VALUES ('" + finalimage + "')";
                            int rs = statement.executeUpdate(strQuery);
                            System.out.println("Query Executed Successfully++++++++++++++");
                            System.out.println("image inserted successfully");
                        } catch (Exception e) {
                        } finally {
                    } catch (Exception e) {

the blank page will appear when i run the code.
could anyone plz help me to solve the problem

Well you have a blank page because you’re using “System.out.println”, which will output to your console. Instead ty using “out.println” as you’ve declared “PrintWriter out” near the top, that will output to the browser. You could also check your console to see what’s actually going on.

Try to use Java console to trace the code:

i already do the pint.out method.
but the result is still same

Yeah but can you check your servers stdout? The reason you’re getting a blank page is because you’re sending everything to System.out which == stdout and != the web browser. (if that makes any sense :confused: )

Of course you can save files and objects in MySQL. It has a pro and cons. For the result, look for rozner’s response.

Excuse me, I can not save files and objects in MySQL. What’s wrong ?:rolleyes: