この方法では何も変更していませんが、突然非常に時間がかかります。以下のコード例では、このエラーが発生します。
編集:私は単一のJavaアプリケーションでメソッドを抽出し、ファイルをロードしようとしましたが、タイムアウトは同じです。3 または 4. 彼がこのループ HttpMethodBase:691 を通過するとき、彼は私のローカル PC で 500 秒間停止し、スレッドはスリープ状態になります。スリープ後、次の行はoutstream.close();
while ((len = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
編集:自宅で試してみたい場合のコード例は次のとおりです:)(httpClient 3.1)
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class TestLoadImage {
/**
* @param args
*/
public static void main(String[] args) {
byte[] image = loadPhotoFromUrl("https://fbcdn-sphotos-a.akamaihd.net/photos-ak-snc1/v2635/232/115/68310606562/n68310606562_2255479_948765.jpg");
System.out.println(image.length);
}
private static class GetMethodIgnoringContentLength extends GetMethod {
public GetMethodIgnoringContentLength(String uri) {
super(uri);
}
@Override
public long getResponseContentLength() {
// ignores content-length header, fakes "not specified":
return -1;
}
}
public static byte[] loadPhotoFromUrl(String photoUrl) {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setBooleanParameter("http.connection.stalecheck", true);
GetMethod get = new GetMethodIgnoringContentLength(photoUrl);
try {
int httpStatus = httpClient.executeMethod(get);
if (httpStatus == HttpStatus.SC_OK) {
byte[] imageBytes = get.getResponseBody();
if (imageBytes.length > 0) {
return imageBytes;
} else {
System.out.println("Failed: empty response/zero data");
}
} else {
System.out.println("Failed: "+ httpStatus);
}
} catch (IOException ioe) {
System.out.println( ioe);
} finally {
get.releaseConnection();
}
return null;
}
}