Chr*_*ail 22
最简单的方法是使用常规文件路径读取它.
在Windows上:
new File("\\\\server\\path\\to\\file.txt")
// (double-backslashes required for backslashes in path)
Run Code Online (Sandbox Code Playgroud)
在Unix上:
首先使用Samba(SMB,NFS或其他任何协议)将共享安装到某个位置,例如/ mnt/network.然后你可以使用:
new File("/mnt/network/path/to/file.txt")
Run Code Online (Sandbox Code Playgroud)
获得File对象后,您可以使用FileInputStream,FileReader或其他任何您想要读取文件的内容.
编辑评论回复.如果您使用的是Applet,则可能需要从Web服务器提取文件.您可以使用内置的java.net.URL类,但如果您需要做的不仅仅是简单的事情,我建议您这样做:http://hc.apache.org/httpclient-3.x/index.html
示例(来自Commons HTTP站点):
// 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)