• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access is Denied

 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my method is not correct how can I change it? I also closed the stream. =)

Thanks so much.

Darren
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you writing

rs.next();

inside the while loop? (First statement within the while loop).Its not required.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic