我总是认为这应该是可能的:
val client = HttpClients.createDefault()
val httpGet = new HttpGet("file:///Users/user01/testfile")
client.execute(httpGet)
抛出:
client: org.apache.http.impl.client.CloseableHttpClient = org.apache.http.impl.client.InternalHttpClient@4ba3987b
httpGet: org.apache.http.client.methods.HttpGet = GET file:///Users/user01/testfile HTTP/1.1
org.apache.http.client.ClientProtocolException: URI does not specify a valid host name: file:///Users/user01/testfile
at org.apache.http.impl.client.CloseableHttpClient.determineTarget(test_ws.sc0.tmp:90)
at org.apache.http.impl.client.CloseableHttpClient.execute(test_ws.sc0.tmp:78)
at org.apache.http.impl.client.CloseableHttpClient.execute(test_ws.sc0.tmp:103)
at #worksheet#.#worksheet#(test_ws.sc0.tmp:6)
当我创建HttpGet
实例时,哪种有意义。
有人知道如何做到这一点吗?
答案 0 :(得分:3)
令人惊讶的是,HttpClient是一个客户端HTTP传输库。它不支持任何其他传输协议。甚至不是本地文件系统。你可能想要的是Apache Commons VFS或类似的东西。
答案 1 :(得分:1)
HttpClient实际上仅用于HTTP,但是您可以使用普通Java来实现相同的目的:
try (BufferedInputStream in = new BufferedInputStream(new URL("file:///tmp/test.in").openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File("/tmp/test.out"))){
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch(IOException e){
e.printStackTrace();
}
答案 2 :(得分:0)
使用内置的java.net.URL
类怎么样?它处理http和文件协议。