gpt4 book ai didi

java - (java) HttpEntity.getContent() 输出有限的 InputStream (8192b/7748b) ...我正在尝试下载 2.4MB 文件

转载 作者:行者123 更新时间:2023-11-30 08:03:42 26 4
gpt4 key购买 nike

我正在使用 Apache HttpClient 4.5,尝试下载 2.4 MB 文件文件可在 Chrome 中使用并完全下载。

问题是,entity.getContent()返回的InputStream仅包含8192字节的缓冲区,而is.available()返回7748. is 的私有(private)归档 contentLength 是 2488649,这应该是所需的文件大小,所以我不明白问题出在哪里。我在调试的变量模块中找到的所有缓冲区大小都是 8192。

尝试用针对我计算机上相同文件的FileInputStream 替换现场的is传输完美,全部 2.4 MB。所以我想我在 http utils 配置上做错了什么。

请帮我解决这个问题。代码看起来有点长,不过应该很容易理解。

CommRunnable。以下类的基类:

public abstract class CommRunnable implements Runnable {
protected HttpPostAgent agent;
protected boolean success;

//...additional methods...//
}

GetFileRunnable。在单独的线程中运行。启动连接和文件传输:

public class GetFileRunnable extends CommRunnable implements Runnable {
private String url;
private String fileDestination;
private NameValuePair[] postPairs;

//...constructor...//

public void run()
{
synchronized (agent) {
try {
HttpClient client = HttpClients.createDefault();

HttpResponse response = agent.getHttpResponse(client, url, postPairs);

InputStream is = response.getEntity().getContent();
FileOutputStream fos = new FileOutputStream(fileDestination);

success = agent.transfer(is, fos);

client.getConnectionManager().shutdown();

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("IOException in HttpPostAgent.getFile");
} finally {
agent.setFree(true);
}
}
}
}

HttpPostAgent。基本上是 Apache http 实用程序的 Controller 。方法在其上方的注释中进行了描述。

public class HttpPostAgent {
private int statusPerc;
private boolean free;

private Thread thread;
private CommRunnable currentAction;

//...constructor...//

//executes POST request and returns resulting HttpResponse
HttpResponse getHttpResponse(HttpClient client, String url, NameValuePair... postPairs)
{
try {
List <NameValuePair> nvps = new ArrayList <NameValuePair>();

for (NameValuePair pair : postPairs)
nvps.add(pair);

HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

return client.execute(post);

} catch (IOException e) {
throw new RuntimeException("IOException in getHttpResponse(HttpClient client, String url, NameValuePair... postPairs)");
}
}

//...methods...//

//Starts thread with GetFileRunnable. Included here for Your understanding of my context.
public void getFileInit(String url, String destinationPath, NameValuePair... postPairs)
{
free = false;
statusPerc = 0;
currentAction = new GetFileRunnable(this, url, destinationPath, postPairs);
thread = new Thread(currentAction);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setDaemon(true);
thread.start();
}

//...methods...//

//Transfers a file from one input stream to the other. For downloading from response/entity/content.
boolean transfer(InputStream is, OutputStream os)
{
boolean success = false;

try {
int remain = is.available();
int total = remain;
int read = 0;
int chunk = 1024;
byte[] buffer = new byte[chunk];

while(remain > 0)
{
if(chunk > remain) chunk = remain;

os.flush();
is.read(buffer, 0, chunk);
os.write(buffer, 0, chunk);

remain -= chunk;
read += chunk;

synchronized (this) {
statusPerc = (read * 100) / total;
}
}
remain = is.available();

success = true;

} catch (IOException e) {
throw new RuntimeException("IOException in HttpPostAgent.transfer");
}

return success;
}

//...methods...//
}

请告诉我是否应该添加更多信息。

最佳答案

摘自InputStream.available()的Javadoc:

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

因此,可用字节不是总输入字节,而是开始读取输入时已缓冲的第一个字节 block 。 8192 似乎是您示例中的缓冲区大小。

因此,您的 HttpPostAgent.transfer 方法实际上仅处理前 8192 个字节,然后停止。

您可以尝试使用以下方法替换 HttpPostAgent.transfer 方法吗?

boolean transfer(InputStream is, OutputStream os) throws IOException
{
byte buffer[] = new byte[2048];
int count;
while ((count = is.read(buffer)) != -1)
os.write(buffer, 0, count);
}

关于java - (java) HttpEntity.getContent() 输出有限的 InputStream (8192b/7748b) ...我正在尝试下载 2.4MB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483815/

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