java从网络设备读取文件 [英] java read file from network device
问题描述
有人可以帮助我找到一个教程,或者从相同网络的任何计算机上读取文件
can someone help me to find a tutorial or sample java code for
的Java代码
推荐答案
最简单的方式是使用常规的文件路径来读取。
The simplest way to do this would be to read it using regular file paths.
在Windows上:
On Windows:
new File("\\\\server\\path\\to\\file.txt")
// (double-backslashes required for backslashes in path)
在Unix上:
首先使用Samba(SMB,NFS或任何其他协议)将共享安装到某些位置,如/ mnt / network。然后你可以使用:
First mount the share using Samba (SMB, NFS or whatever other protocol) to some location like /mnt/network. Then you can use:
new File("/mnt/network/path/to/file.txt")
Once you have the File object you can use FileInputStream, FileReader or whatever else you want to read the file in.
编辑以供评论回复。如果您使用的是Applet,则可能需要从Web服务器中拉取文件。您可以使用内置的java.net.URL类,但如果您不仅要做简单的事情,我会推荐这样做: http://hc.apache.org/httpclient-3.x/index.html
Edit for comments response. If you are using an Applet, you probably want to pull the file from a web server. You can use the built in java.net.URL class but I would recommend this if you have to do more than just simple stuff: http://hc.apache.org/httpclient-3.x/index.html
示例(from Commons HTTP Site):
Example (from the Commons HTTP Site):
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
这篇关于java从网络设备读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!