-2

I'm doing a download from java apps running on linux. The following is the code:

    public void downloadCopyToFile(String filename, String outFilename) throws IOException {
        try {
            URL url = new URL(filename);

            log.info("downloading file from: "+url.toString());
            log.info("export to: "+outFilename);
            
            File outFile = new File(outFilename);
            
            FileUtils.copyURLToFile(url, outFile);          
        } catch (IOException e) {
            throw e;
        }
        
    }

The parameters for the method is as follows:

filename = https://192.168.100.100:8443/generalLedger20240629140353.xlsx outFilename = c:\temp\generalLedger20240629140353.xlsx

When I run this in windows, it can save to c:\temp, but when I move it to the server running linux, it gives me error 500 java.nio.file.AccessDeniedException: c:\temp\generalLedger20240629140353.xlsx

I'm not sure whther copyURLToFile actually understands that this must be written to windows, because if I change the outFilename to /temp/generalLedger20240629140353.xlsx it actually writes to the linux file system.

I have been working on this for the last 3 days. Please help.

9
  • 1
    but when I move it to the server running linux: do I correctly understand that you want the code running on a linux server to download the file to a different computer that uses Windows? How should the linux server know on which Windows computer it should store the file? On my computer or on your computer? Commented Jun 29 at 7:35
  • 2
    Your question is unclear to me. What do you mean by "I'm not sure whther copyURLToFile actually understands that this must be written to windows"? A method does not "understand" anything. This one takes the path you give it and hands it over to the system to get back a file handle where it can save the file contents. If you give it a path in MS-Windows notation this certainly won't work on a linux system. How should a linux system save something to a windows file system? It might be that you mounted some windows file system into your linux file system. But then it is part of that.
    – arkascha
    Commented Jun 29 at 7:37
  • 1
    Keep in mind that the "drive letter" you see in windows path notations does not make any sense in any other system. Even if that method tries to "translate" your windows path notation to a linux path this would not work, unless you actually have a folder /C/temp/ on that system. Which I doubt.
    – arkascha
    Commented Jun 29 at 7:40
  • Why is this question tagged with "apache" and "download"? I suggest to remove both tags.
    – arkascha
    Commented Jun 29 at 7:45
  • 1
    @arkascha Ah - I've typed in wrong text to my comment, I'll delete as not relevant given the path starts c:
    – DuncG
    Commented Jun 29 at 7:54

1 Answer 1

0

If I am reading your question correctly (it is not very clear!) the problem is arising because you are trying to write to a file called

  c:\temp\generalLedger20240629140353.xlsx

on a Linux system.

That didn't work. It would have tried to file whose name started with a c: in the current directory. The exception says that your application didn't have permission to do that. Probably the current directory's ownership, access mode and/or ACL doesn't allow it.

But even if it allowed it, that would have been the wrong thing to do.

Drive letters (like "c:") are only a thing on Windows. On Linux, Unix, MacOS and so forth, "c:" would be interpreted as part of a simple file name.


I'm not sure whether copyURLToFile actually understands that this must be written to windows, because if I change the outFilename to "/temp/generalLedger20240629140353.xlsx" it actually writes to the linux file system.

(Yes, that's the correct way to do it. Assuming you want to write to the Linux file systems.)

Java's copyURLToFile doesn't understand that you want to write that to windows. And even if it understood what you wanted, Java on Linux doesn't know how to write files to Windows. When are running a program on a Linux system, the file system is the local >>Linux<< file system on >>this<< machine, not a file system on some other machine.

But maybe that's not what you mean ...

One possibility is that you have a dual boot machine (Linux and Windows) and you have booted Linux and want to write to the machine's Windows file system. That is possible ... but the Linux OS will need to mount the Windows NTFS (or FAT) file system. You will need to use Linux pathname syntax: no drive letters!

Another possibility is that the Windows file system you are trying to write to is a Windows (SMB) file share. Once again, to do that Linux needs to mount the file share, and you then use Linux pathname syntax to access the files.

5
  • Thanks for the answer. I was reading this baeldung.com/java-download-file and other posting to download file from the server. Any other alternatives to donwload file? Commented Jun 29 at 8:55
  • What I meant was that the ref from baeldung.com/java-download-file and other posting to download file from the server gave me a wrong perception and they are just to copy files. My objective is to produce a file in the server and allows user to download to the local windows machine. Any suggestion to do that? Commented Jun 29 at 9:09
  • I don't understand. When the user runs your program on their Windows machine, they >will< successfully download the file to their local machine. (And on Linux too ... provided that you use a Linux pathname.)
    – Stephen C
    Commented Jun 29 at 9:16
  • I got it now. The above code must be run in a local client windows machine in order to download the file from the server. The code CANNOT run in the server to allow file download to the client. Hopefully this clear the confusion. Commented Jun 29 at 9:22
  • That is correct. A program running on one machine cannot (directly) write to another machine's file system.
    – Stephen C
    Commented Jun 29 at 9:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.