jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Reading file from LAN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading file from LAN" Watch "Reading file from LAN" New topic
Author

Reading file from LAN

Chris Behr
Greenhorn

Joined: Jun 12, 2001
Posts: 17
I am trying to read a file from a server on our LAN using a servlet. Here is the code I have been using:
StringBuffer inBuf = new StringBuffer();
URL inptFile;
try{
inptFile = new URL("file:///\\\\servername\dir\filename");
}
catch(MalformedURLException mfx) {
mfx.toString());
return;
}
URLConnection inptConn = inptFile.openConnection();
inptConn.setDoInput(true);
inptConn.setDoOutput(false);
inptConn.setAllowUserInteraction(false);
inptConn.setUseCaches(false);
String userPassword = "username assword"; // authenticated user
String encodedPassword = URLEncoder.encode(userPassword);
String auth = "Basic " + new sun.misc.BASE64Encoder
().encode (userPassword.getBytes());
inptConn.setRequestProperty("Authorization",auth);
inptConn.getPermission().toString();
inptConn.connect();
InputStream in = inptConn.getInputStream();
int c;
while ((c = in.read()) != -1)
inBuf.append((char)c);
in.close();
}
catch(SecurityException sece) {
sece.toString());
return;
}
catch(IOException ioe) {
ioe.toString());
return;
}

When I run the code here is the output I receive from the inptConn.getPermission().toString()
(java.io.FilePermission \\\servername\dir\filename read)

But then here is the exception after I try to do the inptConn.connect();
java.io.FileNotFoundException: \\servername\dir\filename (Access is denied)
I tried a URL on the same server as my servlet and it works fine. But when I try it on a different file on another server that is when I get the error. I can see the file using file:///\\servername.. from internet explorer. I have even added the code to set the admin user and password with no results. I have turned off all of the security on the web server I could find but it still doesn't work. Is there something I can do to override the security. Any ideas would be appreciated.
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
I think you need to escape/double up all of the back slashes, not just the first ones.
Julia
 
GeeCON Prague 2014
 
subject: Reading file from LAN