Link to home
Start Free TrialLog in
Avatar of ptjha
ptjha

asked on

Access files on Netwrok drive from JBoss

I have a Java application running on JBoss on Windows. I am trying to access files on a network drive. Based on earlier reponses I tried accessing it as
File f = new File("\\computername\folder\filename.pdf");
I can access this file from the Windows "Run" console, but when I try to access it from the browser through my application it says, "<URL path> (Access Denied)"

When the application is running on the same system as where I can invoke the file from "Run" menu command, why can't the application access it? Any ideas?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

As i mentioned earlier, the access will have to be done purely by the 'back end' of the web app. You can't otherwise access anything outside the web folders
... and any SecurityManager run by JBoss will have to allow this
you cannot access a file via a webapp like that, see my suggestions in your earlier question.
Basically it needs to be implemented on your app server (typically as a servlet)
Avatar of ptjha
ptjha

ASKER

Objects,
I am trying to test this in jsp. That is what gave the "access denied" message.

CEHJ,
I am not very familiar with such file handling. Could you elaborate on what you mean by - "the access will have to be done purely by the 'back end' of the web app". I will check the Security Manager setting but what back end access manipualtion are you referring to.

Thanks.
>>but what back end access manipualtion are you referring to.

That's a general point, but since you're using a jsp, let's make it specific: please post the code you're trying
Avatar of ptjha

ASKER

Here's it.
<%@ page language="java" %>
<%@ page import="java.io.File" 
%><%@ page import="java.io.FileInputStream"
%><%@ page import="java.io.InputStream"
%><%@ page import="java.io.OutputStream"
%><%@ page import="java.io.IOException"
%><%
    File f = null;
    InputStream is = null;
    OutputStream os = null;
    try
    {
      f = new File("\\\\server\\folder\\TestPDF.pdf");
      is = new FileInputStream(f);
      os = response.getOutputStream();
      response.setContentType("application/force-download");
      response.setHeader("Content-Disposition", "attachment; filename=\"TestPDF.pdf\"");
    
      int l;
      byte[] b = new byte[1024];
      while(true) 
      {
        l = is.read(b);
        if(l==0) break;
        os.write(b, 0, l);
      }
    }
    catch(IOException ioe)
    { %> <%= e.getMessage() %> <%} 
    finally
    {
      if (is != null)
        is.close();
      if (os != null)
        os.close();
    }
%>

Open in new window

> I am trying to test this in jsp. That is what gave the "access denied" message.

could be that jboss just does not have access to the remote drive.
Make sure you are trying to access it as a file, and not as a url

> Could you elaborate on what you mean by - "the access will have to be done purely by the 'back end' of the web app".

he means what i outlined in your earlier q
Avatar of ptjha

ASKER

Sorry, that was the earlier version. "e.getMessage()" should be replaced with "ioe.getMessage()". I have that in the server version.
first thing to check would be your security manager (try disabling it as a test)
That's probably being disallowed by the container's SecurityManager. If so, you need to change the policy file
Avatar of ptjha

ASKER

Thanks for your quick response. I tested it out by setting the policies in java.policy file, but it still gives "Access is denied" message. Apart from AllPermissions to the SecurityManager, I even added,
 permission java.io.FilePermission "\\\\server\\folder\\TestPDF.pdf", "read, write, execute";
all java.policy. But it still doesn't seem to work.
    Where am I going wrong?

Objects - "could be that jboss just does not have access to the remote drive. Make sure you are trying to access it as a file, and not as a url"
How do I ensure that I am accessing it as file an not URL? Do I need to put "file:///\\\\corp.telephia.com......? Please let me know.
>>I tested it out by setting the policies in java.policy file

Make sure it's the right file and it's being accessed

>>How do I ensure that I am accessing it as file an not URL?

As i mentioned before - you create a File object
Avatar of ptjha

ASKER

>> As i mentioned before - you create a File object

Yes, that's what I am doing. Refering back to the cocde snipet I posted earlier. I am using it as,
 f = new File("\\\\server\\folder\\TestPDF.pdf");

As for the policy file, I made cchanges to the security policy file. Is it mandatory to make these chcanges in the user policy file too i.e. at ${USER.HOME}. Moreover, I haven't been able to locate the user policy file yet. The server has Windows Server 2003 and I did not find the C:\Windows\Profiles so I could not loate the user policy file. Where could it be?
To be on the safe side, just do a global search and check the file stamps
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptjha

ASKER

By default JBoss does not start with Security Manager. So when I tried earlier with Security Manager disabled, it did not work. Now I have made relevant changes to run.bat - JAVA_OPTS to enable security manager and made changes to all java.policy files on the server. But unfortunately it is still not working. I still get access denied and infact it is not an AccessControlException but is an IOException. Not sure why.

Does anybody have any more ideas? I am just stuck at this point.
thought so, didn't seem security manager related.
would appear that jboss just don't have access to the drive

why aren't you mounting the drive in the 1st place?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptjha

ASKER

Objects,
As for fileserver, it is a shared repository and some other applications use it too. So it resides on a separate system. However I did map it as network drive on the app server and tried but that doesn't work as well.

CEHJ,
JBoss service has the following specified as Log On as - Local System Account. I enabled "Allow services to interact with desktop).  As for the file server, folder and files on it, my user id has all read, write, execute and modify provileges. I even set the JBoss Log on to use my credentials which should mean that the application should be able to access the fileserver without any issues. But even then it did not work. Moreover I use the same credentials for the app server and the file server. So that should be simple for the setup as well. So now I can't figure out what else to do or set from my end.

Thanks.
What do you get from

System.out.println(f.canRead());

?
> However I did map it as network drive on the app server and tried but that doesn't work as well.

did u change the path in your code?

> System.out.println(f.canRead());

that will fail
Oh and please post the IOException stack trace
Avatar of ptjha

ASKER

There is no stack trace in it. I am invoking it from jsp and the jsp just has whatever code you see in the above posted snipet. So stack trace does not print anything. The only message for IOException I get is,

"\\server\folder\TestPDF.pdf (Access is denied)"
>>There is no stack trace in it. I am invoking it from jsp ...

The stack trace (if there IS an exception) should appear in your container's log file
Avatar of ptjha

ASKER

It is working now. I did not change anything at our end but had asked the IT group to revisist and reset the permissions on the folders/files. Not sure if that worked because the JBoss user had "Full Control" on the fileserver folders and files from teh beginning.

Thanks for all your help and prompt responses.
OK - glad you've got it