0

I have created below program and program is throwing FileNotFound exception when I pressed the submit button. The problem is coming due to JSP page is not able to find complete path of image. I have debug the JSP program and found that HTML form pass only image name without path that's why problem is coming. Can anyone resolve this problem.

##################  SQL Query ######################################

    CREATE TABLE IMAGEMAIN(ID INTEGER,IMAGE BLOB) ;

##################  HTML  Form ######################

     <form name="frm" method="post" action="index.jsp">
     <input type="text" name="hint">
     <input type="file" name="user_file">
     <input type="submit">

################### JSP PAGE ########################

try
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Connection loaded");
    Connection con = DriverManager.getConnection("jdbc:odbc:project","image","image");
    System.out.println("Connection created");
    String ll=request.getParameter("user_file");
    String lo=request.getParameter("hint");
    File imgfile = new File(ll);

    FileInputStream fin = new FileInputStream(imgfile);

    PreparedStatement pre = con.prepareStatement("insert into IMAGEMAIN (id,image) values(?,?)");
    pre.setString(1,lo);
    pre.setBinaryStream(2,fin,(int)imgfile.length());
    pre.executeUpdate();
    pre.close();
}

catch(Exception E)
{
    out.println("the eror is  "+ E);
}
vishal
  • 43
  • 1
  • 2
  • 9
  • a quick google and here's your solution : http://stackoverflow.com/questions/1142475/jsp-file-upload-with-apache-commons – coding_idiot Aug 19 '13 at 19:59

1 Answers1

0

The constructor for FileInputStream(String) that you're using expects a file name, which won't work because in HTTP uploaded files don't have a filename that works like that - instead you manipulate the stream directly.

According to this SO QA ( jsp file upload issues ) JSP does not provide built-in support for handling multi-part HTTP requests so you cannot handle uploaded files without using an additional Java package such as Apache Commons FileUpload.

So I suggest you install FileUpload, then use that to access the uploaded file. I'm not familiar with it, but there's documentation here: http://commons.apache.org/proper/commons-fileupload/using.html

Community
  • 1
  • 1
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Thanks Dai !! I understand that I need to include Apache Commons FileUpload but I didn't get how to include/write this package in my code. please assist – vishal Aug 19 '13 at 15:51
  • @vishal I can't really help you there - it sounds like you're a beginner to Java. I suggest you read up on some articles and tutorials on Java in general before continuing with JSP/Servlets. – Dai Aug 19 '13 at 17:18