Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading file from LAN

 
Chris Behr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to escape/double up all of the back slashes, not just the first ones.
Julia
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic