Solved

file upload failed using jakarta commons

Posted on 2007-11-23
21
242 Views
Last Modified: 2010-04-21
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 ?
0
Comment
Question by:cofactor
  • 13
  • 5
  • 3
21 Comments
 

Author Comment

by:cofactor
ID: 20341979
anybody here ?
0
 

Author Comment

by:cofactor
ID: 20342006
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 90 total points
ID: 20342215
>> 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
 

Author Comment

by:cofactor
ID: 20343076
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
 
LVL 26

Accepted Solution

by:
mrcoffee365 earned 160 total points
ID: 20344041
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20344450
0
 

Author Comment

by:cofactor
ID: 20348407
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
 

Author Comment

by:cofactor
ID: 20348409
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
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 20348451
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
 

Author Comment

by:cofactor
ID: 20348483
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:cofactor
ID: 20348488
>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
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 20348491
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
 

Author Comment

by:cofactor
ID: 20348500
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
 

Author Comment

by:cofactor
ID: 20348528
trying with     item.getSize()

will update soon
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 20348534
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
 

Author Comment

by:cofactor
ID: 20348568
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
 

Author Comment

by:cofactor
ID: 20348575
time has come to give the points .

do you have any comments ?

shall be closing this question after your comments.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 20348576
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
 

Author Comment

by:cofactor
ID: 20348588
>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
 

Author Closing Comment

by:cofactor
ID: 31410751
excellent
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20348828
?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now