file upload failed using jakarta commons

I uploaded a file from c:\myfile.xls in my jsp .
   

   i have a servlet code like this.. // using jakarat commons upload package

   
   if (item.isFormField()) {
      System.out.println("its a field");
      
      
    String value = item.getString();
    System.out.println(value);
    fields.add(value);
   
   
      } else {
      System.out.println("its a file");
      System.out.println(item.getName());
      File cfile=new File(item.getName()); // item.getName() ==> prints "myfile.xls" ...no c:\ here !
      System.out.println("its a file"+cfile);
      filename=cfile.getName();
      Connection con=(new DBUtil()).getConnection();
      //Statement stmt=con.createStatement();
      InputStream fin = new FileInputStream( cfile ); // FileNotFoundException is thrown from here.


      How to resolve this problem ?
cofactorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mrcoffee365Connect With a Mentor Commented:
The problem is that you are treating the input file as if it exists on your Web server.

Something like this should do what you want:

InputStream fileStream = null;
//your code here until:

  fileStream = item.getInputStream();
0
 
cofactorAuthor Commented:
anybody here ?
0
 
cofactorAuthor Commented:
i think, as the file is not getting c:\  with its name so its not able to create the inputstream and hence throwing file not found exception

But i think , i am doing correctly as per the jakarta commons package example .

am i missing something ?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJConnect With a Mentor Commented:
>> InputStream fin = new FileInputStream( cfile ); // FileNotFoundException is thrown from here.

Well, unless that file is in the current directory on the server, then it will get thrown. Presumably, since a file is being uploaded over the Web, it often (usually) won't be..?
0
 
cofactorAuthor Commented:
not sure what you meant by those words.

basically i have the flow like this....i am highlighting these steps.

*  user uploads a  file from his machine.
*  user clicks on upload button.
*  upload servlet is called // i have posted the above code from that servlet in fact
*  file  converts into  inputstream .
* all these byte  data is going into oracle DB's blob field.

thats my upload in fact . I am NOT going to save file in the server file system  ....i am taking out  the inputstream and pouring the BLOB into oracle DB......and I WAS successful.


believe me, so far, i did not had any problem in uploading files .....in fact , i already have few uploaded files into oracle DB (blob) successfully whih i tested for the past 2-3 days.  and also i wrote a separate download which  downloads those  blob data successfully.......so there were no issues.


But suddenly , today i found the file upload is not working .

i run the debug mode ....and i got a filenotfound exception  on the above code in the debugger  (while  trying to upload a file from jsp)

very much confused whats going on here .

do you see  any mistake in that code so far ?

can i  check this exception ?
 please suggest








0
 
CEHJCommented:
0
 
cofactorAuthor Commented:
ok.
i added this line

fileStream = item.getInputStream();

and i see , some entry in the DB.

but when i  try download this BLOB , i get

Error 500--Internal Server Error

java.lang.NullPointerException
        at java.io.OutputStream.write(OutputStream.java:58)
0
 
cofactorAuthor Commented:
i think , the upload is not working.

because , my download code works fine.
i have another blob and my download code downloads them perfectly.

hence,something wrong in this upload
0
 
mrcoffee365Commented:
We need to see the code you are using for your upload (now) and how you load it into the database.  Putting a .xls file as a blob into a db is a different question from getting file upload to work.

If you would like to check to see if you are getting the .xls file on your server, you can write the file out as well as save it in the database.  Create a local directory on the server for uploaded files, and save it there.  If the .xls file is fine, then the problem is how you are loading it into the database.
0
 
cofactorAuthor Commented:
however , i debugged when i tried to download this code .

see java.lang.NullPointerException
        at com.pwc.util.DownloadUtil.getFile(DownloadUtil.java:104)

                fileSize = (int)tmpBlob.length();  // line 104


evidently , the  the BLOB did not uploaded properly
0
 
cofactorAuthor Commented:
>If you would like to check to see if you are getting the .xls file on your server, you can write the file out as >well as save it in the database.  Create a local directory on the server for uploaded files, and save it there.  >If the .xls file is fine, then the problem is how you are loading it into the database.

i did not wanted to dump file in the server .

i wanted to do it  straight way to the DB

   
0
 
mrcoffee365Commented:
Yes, I know that you want the application to ultimately only save the file to the db.  But in debugging, it can help set your mind at rest that the file is available on your server, and point you towards debugging the insert into the db.

Or, you might find that something is not uploading correctly.  We use file upload a lot and never have a problem, but there might be something in your code which needs to change.
0
 
cofactorAuthor Commented:
ok.
i got something probabily

here is the code

System.out.println("its a file");
      System.out.println(item.getName());
      File cfile=new File(item.getName());
      
      filename=cfile.getName();
      Connection con=(new DBUtil()).getConnection();
      
      InputStream fin = item.getInputStream();
      String sql = "my sql query here";
      PreparedStatement ps = con.prepareStatement(sql);
   
     try {
          Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery("get the sequence query");
     
       
         while (rs.next()) {
              seqNo = rs.getString(1);
            }
     } catch (SQLException e) {e.printStackTrace();
     }
      
     
     
       ps.setString(1, seqNo);
       ps.setString(2,(String)fields.get(1) );
       ps.setString(3,(String)fields.get(2));
       ps.setString(4, filename);
       ps.setBinaryStream(5, fin, (int)cfile.length()); // mistake is here ?
      
       
       
       
       ps.executeUpdate();
       ps.close();
       fin.close();
       con.commit();
       con.setAutoCommit(true) ;





see the above code , i used  ps.setBinaryStream(5, fin, (int)cfile.length()); // mistake is here ?

 item.length()  does not exist.

mistake is here ?

how to rectify it ?
0
 
cofactorAuthor Commented:
trying with     item.getSize()

will update soon
0
 
mrcoffee365Commented:
Do you see where you're still trying to read from a file on your local server?  In this line:

ps.setBinaryStream(5, fin, (int)cfile.length()); // mistake is here ?

you try to read from cfile, which CEHJ and I both pointed out doesn't exist on your server.  I suggested you read in your uploaded file to an InputStream, which you did -- then ignored it.

If you want to read in the file to input it into the db, then write out the item InputStream to a file on your local server and read it.

Or just use the InputStream object as your binary stream in ps.setBinaryStream.

>> item.length()  does not exist.
Right -- you have to use the getSize() method.
0
 
cofactorAuthor Commented:
yea..its working now
item.getSize()  was the saviour :-)

BTW, i kept

File cfile=new File(item.getName());
     
      filename=cfile.getName();

just to get the filename .

ite,.getName()  gives the filename with directory .

i just need the filename so made a fileobject and get the name only from there.

i cant remove it.


its working fine now
 
0
 
cofactorAuthor Commented:
time has come to give the points .

do you have any comments ?

shall be closing this question after your comments.
0
 
mrcoffee365Commented:
Great -- congrats and good luck.

By the way, the file name is here:
item.getName()
so you don't have to keep the file manipulation.
0
 
cofactorAuthor Commented:
>item.getName()

yea..i tried it.....but it comes with directory also .
like c:\myfile.xls

i dont like c:\  and also i did not wanted to parse and remove this string....so just used file object ...because that gives only the filename without directory
0
 
cofactorAuthor Commented:
excellent
0
 
CEHJCommented:
?
0
All Courses

From novice to tech pro — start learning today.