gpt4 book ai didi

java - 为浏览器提供文件下载的输入流

转载 作者:可可西里 更新时间:2023-11-01 17:10:27 24 4
gpt4 key购买 nike

<分区>

我有一个输入流连接到服务器上的一个文件。输入流是使用 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();
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com