0

この方法では何も変更していませんが、突然非常に時間がかかります。以下のコード例では、このエラーが発生します。

編集:私は単一の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;
    }
}
4

1 に答える 1

0

HttpClient4.1 への更新で修正しました (およびそのために必要なリファクタリング)。しかし、誰かが上記の例の理由を知っている場合は、正解を切り替えます。

于 2011-08-04T08:40:12.900 に答える