我有一个输入流连接到服务器上的文件。使用Apache Web Components建立输入流。如何将该输入流提供给用户的浏览器,以便使用Apache Web Components在浏览器中下载文件?
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("user", "pass"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpGet httpget = new HttpGet("https://website.com/file.txt");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
InputStream in=entity.getContent();
int c;
while((c=in.read())!=-1){
//maybe write to an ouput stream here so file can download?
System.out.println(c);
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
答案 0 :(得分:2)
只是另一个HTTP框架:
也许这会有所帮助:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
// do something useful
} finally {
instream.close();
}
}
} finally {
response.close();
}
Quelle:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e49
HttpEntity Instanz为您提供了一个可以使用标准Java Streaming类和方法进行评估的InputStream。
可能是一个答案,如果不是pls提供代码片段或具体你的问题。