15

I have a file copied in one computer and I need to access the file from other computer. I am not sure, which protocol or which technology to use for this? Please provide me any hints for this..

Update:

I am using Ubuntu Linux system. I used the code :

File f = new File("//192.168.1.157/home/renjith/picture.jpg");// 192.168.1.157 is the ip of the computer, where I have the picture file
Image image = ImageIO.read(f);

But it is giving an exception:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)

I have shared renjith folder also.

8 Answers 8

10

There are any number of ways to access files on remote machines, but they virtually all depend on the remote machine having been set up to provide the file in some way first. If you with to access files via java, the easiest method would probably be to set up an HTTP server on the remote machine (this can be done pretty easily using Apache HTTP server on a variety of platforms) and then using Apache Commons HTTPClient on the client side java app. Further discussion of how to install these or configure them is generally beyond the scope of Stack Overflow and would at least require a more specific question

Sign up to request clarification or add additional context in comments.

Comments

6

HTTP is an option. However, if these are Windows machines on the same LAN, it would be easier to expose the directory on the remote machine via a file share and access the file through a regular file path. Similarly, if these are Unix-like machines, you could use regular file paths if you're using NFS. FTP's yet another option.

Comments

6

if the remote computer is in the same network and on a shared folder to the computer where your java code is running then try this piece of code for accessing it

File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");

here Comp-1 is the DNS name of the machine containing the file in the network!!!

1 Comment

and you can also use the ip in place of DNS name i.e. replace Comp-1 with 192.168.1.157
6

You might try:

URL url = new URL("file://192.168.1.157/home/renjith/picture.jpg");
Image image = ImageIO.read(url); 

1 Comment

Exactly. Only correct answer. "file://192.168.1.157/home/renjith/picture.jpg" is a URL, not a filename.
2

You could try to mount that path first, and then load it. Do a :

subst x: \\192.168.1.157

and then:

File f = new File("x:\\home\\renjith\\picture.jpg");
Image image = ImageIO.read(f)

It should work.

Comments

0

Share the directory and access the file thruogh java code try this one:

File f = new File("//10.22.33.122/images")

File[] files = f.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        // Specify the extentions of files to be included.
        return name.endsWith(".bmp") || name.endsWith(".gif");
    }
});

// get names of the files
String[] fileNamesArray = null; 
for (int indx = 0; indx < files.length(); indx++) {
    fileNamesArray[indx] = files[indx].getName();
}

return fileNamesArray; 

1 Comment

Hi All, I am using Ubuntu Linux system. I used the code : File f = new File("//192.168.1.157/home/renjith/picture.jpg");// 192.168.1.157 is the ip of the computer, where I have the picture file Image image = ImageIO.read(f); But it is giving an exception: javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(ImageIO.java:1275) I have shared renjith folder also. Any help is appreciable. Thanks In Advance..
0

Map your IP to network drive and try let us say the drive letter is X,

then code changes to File f = new File("x:\\home\\renjith\\picture.jpg");

Infact your file is already loaded in object f , try priting the value of the path f.getAbsolutePath() to console and see.. Actual error is with ImageIO

1 Comment

this dont make sense. File is only a wrapper for any file operation, the exists() api can tell you if file is there or not.
0

You can read from remote and write to remote using jcifs-1.3.15.jar jar in java but first you need to share location from remote system then it's possible.

try{
            String strLine="";    
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("WORKGROUP", "username", "passwd"); // Authentication info here, domain can be null
    //        try (InputStream is = new SmbFile("smb://DESKTOP-0xxxx/usr/local/cache/abc.txt", auth).getInputStream()) {
            try (InputStream is = new SmbFile("smb://xx.xx.xx.xxx/dina_share/abc.txt", auth).getInputStream()) {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while ((strLine = br.readLine()) != null) {
                System.out.println(strLine);
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
            String smbURL="smb://xx.xx.xx.xxx/dina_share/abcOther.txt";
            SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL,auth));
            byte bytes[]="Wellcome to you".getBytes();
            fos.write(bytes);
        }catch(Exception e){
            e.printStackTrace();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.