• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way for a Servlet to access file

 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Tomcat 4.1 and Win 2003, I had a Servlet app that read 15,000 integers from a data file on the local Server hard drive. With Tomcat 7 and Win 2012, my Servlet gets error messages (java.io.FileNotFoundException) trying to read from the local drive. What would be the best way to access data on the Server with a Servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are getting a file-not-found exception, then the path to the file is wrong, or the file does not exist. Your next step is to determine which.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Tomcat 4? Might be time to think of an upgrade.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'll post the code of the class that does the file i/o we can probably spot the path problem. The file i/o shouldn't be in the servlet, but we'll deal with that later.

Make sure the file does actually exist. It's possible that whatever routine creates that file has failed.
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I can post the code. I read in 2 arrays that are both 5x6: so the number of elements is 6x6x6x6x6 = 7,776. I thought if I included the arrays in the .java file itself, I wouldn't have to worry about the issues with reading from files.
I access the local C: drive file with
I get the error msg
Error: java.io.FileNotFoundException (the system cannot find the file specified)

Remember that for 5 years I never had a problem: I suspect Win 2012 is not giving permission rights for Tomcat 7 to read a file on the local host.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you check that the file exists? If it does, are the permissions correct?
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Racin wrote:
Remember that for 5 years I never had a problem: I suspect Win 2012 is not giving permission rights for Tomcat 7 to read a file on the local host.


I suspect this is correct. But is it Tomcat 4 or 7? You've mentioned both.

As Bear says, make sure the file exists and that the account running the web application (tomcat, probably) has the necessary file permissions. The file permissions have to be granted to the account that is running the Tomcat service.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:As Bear says, make sure the file exists and that the account running the web application (tomcat, probably) has the necessary file permissions. The file permissions have to be granted to the account that is running the Tomcat service.


And as a follow-up to that statement, you must be running the Tomcat service under an actual account and not under "Local System Account" or some other pseudo-account. Even if you grant permissions for the file to "Everybody" that still won't allow those pseudo-accounts to access the file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic