cofactor
asked on
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.ge tName());
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 ?
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.ge
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 ?
ASKER
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 ?
But i think , i am doing correctly as per the jakarta commons package example .
am i missing something ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.NullPointerExcep tion
at java.io.OutputStream.write (OutputStr eam.java:5 8)
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.NullPointerExcep
at java.io.OutputStream.write
ASKER
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
because , my download code works fine.
i have another blob and my download code downloads them perfectly.
hence,something wrong in this upload
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.
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.
ASKER
however , i debugged when i tried to download this code .
see java.lang.NullPointerExcep tion
at com.pwc.util.DownloadUtil. getFile(Do wnloadUtil .java:104)
fileSize = (int)tmpBlob.length(); // line 104
evidently , the the BLOB did not uploaded properly
see java.lang.NullPointerExcep
at com.pwc.util.DownloadUtil.
fileSize = (int)tmpBlob.length(); // line 104
evidently , the the BLOB did not uploaded properly
ASKER
>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
i did not wanted to dump file in the server .
i wanted to do it straight way to the DB
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.
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.
ASKER
ok.
i got something probabily
here is the code
System.out.println("its a file");
System.out.println(item.ge tName());
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)fie lds.get(1) );
ps.setString(3,(String)fie lds.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 ?
i got something probabily
here is the code
System.out.println("its a file");
System.out.println(item.ge
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)fie
ps.setString(3,(String)fie
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 ?
ASKER
trying with item.getSize()
will update soon
will update soon
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.
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.
ASKER
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
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
ASKER
time has come to give the points .
do you have any comments ?
shall be closing this question after your comments.
do you have any comments ?
shall be closing this question after your comments.
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.
By the way, the file name is here:
item.getName()
so you don't have to keep the file manipulation.
ASKER
>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
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
ASKER
excellent
?
ASKER