如何使用java从远程系统读取文件?

我在一台计算机上复制了一个文件,我需要从其他计算机访问该文件。 我不确定,使用哪种协议或哪种技术? 请给我任何提示……

更新:

我正在使用Ubuntu Linux系统。 我用过代码:

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); 

但它给出了一个例外:

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

我也共享了renjith文件夹。

有许多方法可以访问远程计算机上的文件,但它们几乎都依赖于已经设置的远程计算机以先以某种方式提供文件。 如果您要通过java访问文件,最简单的方法可能是在远程计算机上设置HTTP服务器(这可以通过各种平台上的Apache HTTP服务器轻松完成),然后使用Apache Commons HTTPClient客户端Java应用程序。 有关如何安装或配置它们的进一步讨论通常超出了Stack Overflow的范围,并且至少需要更具体的问题

HTTP是一种选择。 但是,如果这些是同一LAN上的Windows计算机,则通过文件共享公开远程计算机上的目录并通过常规文件路径访问该文件会更容易。 同样,如果这些是类Unix机器,如果您使用NFS,则可以使用常规文件路径。 FTP是另一种选择。

如果远程计算机位于同一网络中,并且位于运行Java代码的计算机的共享文件夹中,请尝试使用此代码访问它

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

这里Comp-1是包含网络文件的机器的DNS名称!

你可以尝试:

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

您可以先尝试安装该路径,然后加载它。 做一个:

 subst x: \\192.168.1.157 

接着:

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

它应该工作。

共享目录并访问文件thruogh java code试试这个:

 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; 

您可以使用java中的jcifs-1.3.15.jar jar从远程读取并写入远程,但首先您需要从远程系统共享位置然后才可能。

 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(); } 

将您的IP映射到网络驱动器,然后让我们说驱动器号是X,

然后代码更改为File f = new File("x:\\home\\renjith\\picture.jpg");

实际上你的文件已经加载到对象f ,尝试将路径f.getAbsolutePath()的值f.getAbsolutePath()到控制台并查看..实际错误与ImageIO