0

Drop the table you have and create a new one.

i had been follow your advised.
drop and make a new table.
the problem is not settle.
the image still not save in the database and when i click submit,
it tooks about 4 minutes to loading but the image still not save in the database.
am I too bad?:'(

0

my code :
********uploadImage.jsp*************

<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;">
      <tr>
        <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
           Image Details</td>

******UploadImage.java******

import java.io.*;
import java.sql.*;
import java.util.*;
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 {
    @Override
    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) {
                e.printStackTrace();
            }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                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);
                        item.write(savedFile);
                        out.println("<html>");
                        out.println("<body>");
                        out.println("<table><tr><td>");
                        out.println("<img src=images/" + finalimage + ">");
                        out.println("</td></tr></table>");

                        Connection conn = null;
                        String url = "jdbc:mysql://localhost:3306/";
                        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);
                            Class.forName(driver).newInstance();
                            conn = DriverManager.getConnection(url + dbName, username, userPassword);
                            Statement st = conn.createStatement();
                            strQuery = "insert into testimage values image='" + finalimage + "'";
                            int rs = st.executeUpdate(strQuery);
                            System.out.println("Query Executed Successfully++++++++++++++");
                            out.println("image inserted successfully");
                            out.println("</body>");
                            out.println("</html>");
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        } finally {
                            conn.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

ERROR :
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

hi

<html>

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

<body>
  <form action="insertImage11.jsp" method="post" enctype="multipart/form-data"  name="productForm" id="productForm"><br><br>
<table width="400px" align="center" border=0 style="background-color:ffeeff;"><tr>
<td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">Image Details</td>
</tr>
<tr>
<td align="center" colspan=2>&nbsp;</td>
</tr>
<tr>
<td>Image Link: </td>
 <td>
<input type="file" name="file" id="file">
<td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
  <tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.ServletException.*" %>
<%@ page import="javax.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page language="java" import="java.sql.*;"%>
<%			
 Connection connection = null;
 String connectionURL = "jdbc:mysql://localhost/vrc";
 ResultSet rs = null;
 PreparedStatement pre = null;
 try
  {				
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "javavrc", "vrc07");
//FileUpload fup = new FileUpload();
//FileUpload fup=new FileUpload();
//DiskFileUpload upload=new DiskFileUpload();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();

int count = 0;
while (iter.hasNext()) {
count++;
FileItem item = (FileItem) iter.next();
File cfile = new File(item.getName());
File tosave = new File(getServletContext().getRealPath("/temp/"), cfile.getName());
item.write(tosave);
String file_name = item.getName();
FileInputStream fis = new FileInputStream(tosave);
int len = (int) tosave.length();
pre= connection.prepareStatement("insert into image(imageName, image) "+ "values(?,?)");
pre.setString(1,"Delhisswa");
fis = new FileInputStream(tosave);
//image_data column holds LONGBLOB data type

pre.setBinaryStream(2,fis,(int)tosave.length());
int rows = pre.executeUpdate();
}
}
    
    catch (Exception ex)
    	
    {
      out.println(ex.getMessage());
  }
  %>

this code use for u .

0

swathirao,

Use code tags. Source code must be surrounded with code tags.
For example,

[CODE=Jsp] ... statements..

[/code]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.