0

编写代码将文件上传到文件主机,但速度非常慢,我使用的是最新的 jar。上传速度比使用浏览器慢 10 倍。什么可能导致这个问题?

        HttpPost httppost = new HttpPost(xxxx);

        CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, new CustomMultiPartEntity.ProgressListener() {

            @Override
            public void transferred(long num) {
                item.setProgress((int)num / 1024);
            }
        });

        ContentBody cbFile = new FileBody(item.getFile());

        multipartContent.addPart("uploads[]", cbFile);

        httppost.setEntity(multipartContent);

        HttpResponse response = clientManager.getClient().execute(httppost, localContext);
        HttpEntity entity = response.getEntity();
        String htmlResponse = EntityUtils.toString(entity);

我的客户代码在这里:

private DefaultHttpClient client;

私有最终 HttpParams 参数 = 新 SyncBasicHttpParams();

public ClientManager()
{
    ClientConnectionManager connectionManager = new ThreadSafeClientConnManager();
    ((ThreadSafeClientConnManager) connectionManager).setDefaultMaxPerRoute(30);
    ((ThreadSafeClientConnManager) connectionManager).setMaxTotal(1000);

    CookieSpecFactory csf = new CookieSpecFactory()
    {
        @Override
        public CookieSpec newInstance(final HttpParams params)
        {
            return new BrowserCompatSpec()
            {
                @Override
                public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException
                {
                }
            };
        }
    };

    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, 40 * 1000);
    HttpConnectionParams.setSoTimeout(params, 40 * 3000);
    HttpConnectionParams.setTcpNoDelay(params, true);

    //HttpConnectionParams.setSocketBufferSize(params, 10485760);

    params.setParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true);
    params.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/10.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/20.0");
    params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

    client = new ContentEncodingHttpClient(connectionManager, params);

    client.setHttpRequestRetryHandler(new HttpClientRequestRetryHandler());
    client.getCookieSpecs().register("EASY", csf);
    params.setParameter(ClientPNames.COOKIE_POLICY, "EASY");
}

}

4

0 回答 0