gpt4 book ai didi

java - HttpClient 请求成功并定义了超时,但没有定义就挂起

转载 作者:行者123 更新时间:2023-12-04 15:53:18 26 4
gpt4 key购买 nike

我发现了一种奇怪的 hack-y 方法来修复我的代码,我想知道是否有人可以解释它为什么有效。我正在编写与 REST API 通信的代码,以上传拆分为多个 HTTP 请求的视频文件。

我的一个视频部分请求连接时遇到问题,但从未响应。程序分五段上传视频,但总是卡在五段的第三段。我决定添加一个请求硬超时来强制程序跳过那个挂起的部分。好吧,神奇地添加了那个计时器之后,就没有挂断电话了!

知道为什么会这样吗?请求实际上并没有超时,但添加此代码会使我的程序继续运行。

private void uploadParts(String assetId) throws IOException {
//set up post request
HttpClient client = HttpClientBuilder.create().build();

String url = "";

//prepare video
File video = new File("files/video.mp4");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(video));

int partMaxSize = 1024 * 1024 * 5;
byte[] buffer = new byte[partMaxSize];
double fileSize = video.length();
System.out.println(fileSize);
System.out.println(fileSize / partMaxSize);
int parts = (int) Math.ceil(fileSize / partMaxSize);
System.out.println(parts);

for(int i = 1; i < parts+1; i++) {
String partNumber = i + "";
System.out.println("part: " + partNumber);
int partSize = (int) (i < parts ? partMaxSize : fileSize);
fileSize -= partSize;
int tmp = 0;
tmp = bis.read(buffer);
url = String.format("https://www.site.com/upload/multipart/%s/%s", assetId, partNumber);

final HttpPut request = new HttpPut(url);
request.addHeader("Authorization", "Bearer " + accessToken);
request.addHeader("Content-Type", "application/octet-stream");
request.setEntity(new ByteArrayEntity(buffer));

//Magical code start
int hardTimeout = 5; // seconds
TimerTask task = new TimerTask() {
@Override
public void run() {
if (request != null) {
request.abort();
}
}
};
new Timer(true).schedule(task, hardTimeout * 1000);
//Magical code end

HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getReasonPhrase());
}
bis.close();
}

如果我遗漏了魔法代码部分,我的代码将卡在第三部分。如果我包含它,程序会正常运行。

最佳答案

我找到答案了!原来 HttpClient 一次只允许一定数量的连接。根据我的代码,默认的最大连接数是 2。我需要在每个连接完成并且上传正常后关闭它们。

固定代码添加请求连接释放。

private void uploadParts(String assetId) throws IOException {
//set up post request
HttpClient client = HttpClientBuilder.create().build();

String url = "";

//prepare video
File video = new File("files/video.mp4");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(video));

int partMaxSize = 1024 * 1024 * 5;
byte[] buffer = new byte[partMaxSize];
double fileSize = video.length();
System.out.println(fileSize);
System.out.println(fileSize / partMaxSize);
int parts = (int) Math.ceil(fileSize / partMaxSize);
System.out.println(parts);

for(int i = 1; i < parts+1; i++) {
String partNumber = i + "";
System.out.println("part: " + partNumber);
int partSize = (int) (i < parts ? partMaxSize : fileSize);
fileSize -= partSize;
int tmp = 0;
tmp = bis.read(buffer);
url = String.format("https://www.site.com/upload/multipart/%s/%s", assetId, partNumber);

final HttpPut request = new HttpPut(url);
request.addHeader("Authorization", "Bearer " + accessToken);
request.addHeader("Content-Type", "application/octet-stream");
request.setEntity(new ByteArrayEntity(buffer));

//Magical code start
int hardTimeout = 5; // seconds
TimerTask task = new TimerTask() {
@Override
public void run() {
if (request != null) {
request.abort();
}
}
};
new Timer(true).schedule(task, hardTimeout * 1000);
//Magical code end

HttpResponse response = client.execute(request);
request.releaseConnection();
System.out.println(response.getStatusLine().getReasonPhrase());
}
bis.close();
}

计时器正在工作,因为它会在 10 秒后关闭我的旧连接。伙计们,谢谢你们的意见。

关于java - HttpClient 请求成功并定义了超时,但没有定义就挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24498964/

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