如何使用java从远程系统读取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2011264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a file from remote system using java?
提问by user236919
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 :
我正在使用 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);
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.
我也共享了 renjith 文件夹。
回答by Jherico
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
有多种方法可以访问远程机器上的文件,但它们实际上都取决于已设置的远程机器首先以某种方式提供文件。如果您要通过 java 访问文件,最简单的方法可能是在远程机器上设置一个 HTTP 服务器(这可以很容易地在各种平台上使用 Apache HTTP 服务器完成),然后在远程机器上使用 Apache Commons HTTPClient客户端 Java 应用程序。关于如何安装或配置它们的进一步讨论通常超出了 Stack Overflow 的范围,至少需要一个更具体的问题
回答by Jacob
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.
HTTP 是一种选择。但是,如果这些是同一 LAN 上的 Windows 计算机,则通过文件共享公开远程计算机上的目录并通过常规文件路径访问文件会更容易。类似地,如果这些是类 Unix 机器,那么如果您使用 NFS,则可以使用常规文件路径。FTP 的另一种选择。
回答by srinannapa
Share the directory and access the file thruogh java code try this one:
共享目录并通过 java 代码访问文件试试这个:
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;
回答by Geo
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.
它应该工作。
回答by srinannapa
Map your IP to network drive and try let us say the drive letter is X,
将您的 IP 映射到网络驱动器并尝试让我们说驱动器号是 X,
then code changes to File f = new File("x:\\home\\renjith\\picture.jpg");
然后代码更改为 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
事实上,您的文件已经加载到 object 中f
,尝试将路径的值打印f.getAbsolutePath()
到控制台并查看..实际错误是ImageIO
回答by Alivia
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
如果远程计算机与运行 Java 代码的计算机在同一网络中并位于共享文件夹中,请尝试使用这段代码来访问它
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!!!
这里的 Comp-1 是网络中包含文件的机器的 DNS 名称!!!
回答by Paul Hymanson
You might try:
你可以试试:
URL url = new URL("file://192.168.1.157/home/renjith/picture.jpg");
Image image = ImageIO.read(url);
回答by Dinanath Parit
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.
您可以使用 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();
}