hi ,


I used the below code to upload the pictures to database through jsp code. It works fine when i browse the images through temp folder of my working directory. In that code i got a two problems as

upload.jsp

Code:
<%@ page import="java.io.File"%>;
<%@ page import="java.io.FileInputStream"%>;
<%@ page import="java.io.IOException" %>;
<%@ page import="java.sql.Connection"%>;
<%@ page import="java.sql.DriverManager"%>;
<%@ page import="java.sql.PreparedStatement"%>;
<%@ page import="java.sql.ResultSet"%>;
<%@ page import="java.sql.Statement"%>;
<%@ page import="java.util.Iterator"%>;
<%@ page import="java.util.List"%>;
<%@ page import="javax.servlet.ServletException"%>;
<%@ page import="javax.servlet.http.HttpServlet"%>;
<%@ page import="javax.servlet.http.HttpServletRequest"%>;
<%@ page import="javax.servlet.http.HttpServletResponse"%>;
<%@ page import="org.apache.commons.fileupload.*"%>;
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*, java.util.*, java.io.*, java.lang.Exception" %>
 
<%
String connectionURL = "jdbc:mysql://localhost:3306/";
String dbName = "user_register";
Connection connection = null;
PreparedStatement pre=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL+dbName, "root", "root");
out.println("connection");
FileUpload fup = new FileUpload();
            DiskFileUpload upload = new DiskFileUpload();
            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 (image)values(?)");
                //image_data column holds LONGBLOB data type
               
                 pre.setBinaryStream(1,fis,(int)tosave.length());
                int rows =  pre.executeUpdate();
            }
 
 %>
1. I can browse the images from only temp folder of my working directory and i upload the images from other folder it shows error messages as "file not found"

2. When i use the html code like that

Code:
<form name="form-upload" action="POST" action="upload.jsp"  enctype="multipart/form-data">
 <input type=file name="ufile">
 <input type="submit" name="submit" value="submit">
 </form>
It works fine. And i added the another filed to form as image name it didn't work. And shows errors

like that

Code:
<form name="form-upload" action="POST" action="upload.jsp"  enctype="multipart/form-data">
Image Name<input type="text" name="imagename" value="">
<input type=file name="ufile">
<input type="submit" name="submit" value="submit">
</form>
I used like that i got the error as


org.apache.jasper.JasperException: Exception in JSP: /upload4.jsp:37

34: while (iter.hasNext()) {
35: count++;
36: FileItem item = (FileItem) iter.next();
37: File cfile = new File(item.getName());
38: File tosave = new File(getServletContext().getRealPath("/temp/"), cfile.getName());
39: //item.write(tosave);
40: // String file_name = item.getName();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)

root cause

java.lang.NullPointerException
java.io.File.<init>(Unknown Source)
org.apache.jsp.upload4_jsp._jspService(upload4_jsp .java:99)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


Please tell that solution.How can i modified my code to resolve that 2 problems