File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Access is Denied Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Access is Denied" Watch "Access is Denied" New topic
Author

Access is Denied

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

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.

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Darren Alexandria:
I have a feeling that my error is in the FileOutputStream but I am not that sure. Please give me an advice.


You should post the stack trace and show us the line number where the error occurs
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Try changing
FileOutputStream fos = new FileOutputStream("C:/DownloadedFiles");

to

FileOutputStream fos = new FileOutputStream("C:\\DownloadedFiles\\sometext.txt");

and see whether the file is getting created or not.

As mentioned by David , post the stacktrace.

Just curious to know whether are you closing the opened streams before the program completes.


Rahul Bhattacharjee
LinkedIn - Blog
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
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
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
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
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?
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


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
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: 2308
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 ?

FileOutputStream fos = new FileOutputStream("C:\\DownloadedFiles\\TestFile.bin");

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
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
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
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: 2308
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
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
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

fos = new FileOutputStream(path, true);

PrintWriter pout = null;
pout = new PrintWriter(new FileOutputStream(path,true));

while ((i = is.read()) != -1)
{
pout.write(i);
}
//fos.write(i);
//pout.write(i);
fos.close();
pout.close();

}// end of if construct
}// end of try

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.
 
GeeCON Prague 2014
 
subject: Access is Denied