My task is to download Blob items from the database and save it to a specified directory. When I run the program I get an "Access is Denied" error. What am i suppose to change? I have a feeling that my error is in the FileOutputStream but I am not that sure. Please give me an advice. Thanks so much.
I am sorry for forgetting to post the Stack Trace. Here it is:
java.io.FileNotFoundException: C:\DownloadedFiles (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:179) at java.io.FileOutputStream.<init>(FileOutputStream.java:70) at org.apache.jsp.viewBlob_jsp._jspService(viewBlob_jsp.java:104) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685) at java.lang.Thread.run(Thread.java:595) java.io.FileNotFoundException: C:\DownloadedFiles (Access is denied)
Thanks so much for your replies. I am having a hard time because this is the first time I have used the FileOutputSteam class.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
Mr.Rahul Bhattacharjee,
I used the code you gave which is:
and it did get created in the specified directory.
What does this mean?
Thanks so much. God bless all of you.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
By the way does my code say that it will get the blob files from the database, convert it back to file format and save it to C:/DownloadedFiles folder?
Or if not, what does it say?
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
just found out that when I change the
TO:
The first 4 lines of the first blob entry in the database is being copied. No errors.
How can I make all of the blob files from the database be downloaded in the local directory with its original filename?
How can I make all of the blob files from the database be downloaded in the local directory with its original filename?
Normally you will need to store the filename in the database.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
Thanks again Mr. Freddy Wong. I have appended the filename after being retrieved in the database.
The problem is only one of the files from the database is downloaded when it supposed to be all of them. And only the first 4 lines of the original file is being copied to the downloaded file. Why is that so?
Thanks again to you all.
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
posted
0
Originally posted by Darren Alexandria: just found out that when I change the
TO:
The first 4 lines of the first blob entry in the database is being copied. No errors.
How can I make all of the blob files from the database be downloaded in the local directory with its original filename?
Hi Darren ,
The above is not going to work , by just appending the inputstream with the file name.
What you have done previously is correct. I think you want to store all the bytes from a BLOB in a file in the local filesystem.Is that correct ?
and close the streams after the program. And see what happens , if encounter any exception then mail the stact trace again.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
Yes, I have 10++ files in the database and I want to retrieve them all. The reason why I appended the string "filename" is that I want to get the real or original filename of the blobs in the database.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
If my method is not correct how can I change it? I also closed the stream. =)
Thanks so much.
Darren
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
Hi Rahul,
This is my latest code:
it writes the file with the correct filename from the database but again it only gets 104bytes of the file.
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
posted
0
Why are you writing
rs.next();
inside the while loop? (First statement within the while loop).Its not required.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
Sorry that's my Typographical Error. I have a new code which now copies all the files in the database to the folder. However, all of them are of 104 bytes in size.
Darren Alexandria
Ranch Hand
Joined: Aug 17, 2007
Posts: 185
posted
0
This is my latest code:
[CODE] while (rs.next()) {
String filename = rs.getString(1); Blob blob = rs.getBlob(2); InputStream is = null; is = blob.getBinaryStream(); int i; String dir = "C:\\DownloadedFiles"; File path = new File(dir, filename); FileOutputStream fos = null; //place to store the stream reference
It download 16 files from the database. The weird thing here is that only file # 4,5,6 and 7 have the correct contents. The rest are not complete. I don't know how it happened.