【问题标题】:How can I improve the file upload speed for big size?如何提高大尺寸文件的上传速度?
【发布时间】:2019-06-11 13:52:23
【问题描述】:

我正在开发 Android 操作系统上的文件上传应用程序。

基本上我使用的是 HttpURLConnection,它是必需的。

文件大小差不多,IOS很快,我用的是AFNetworking。

但是Android太慢了,请指教我缺少什么。

这是我使用的源代码。

谢谢。

        HttpURLConnection conn = null;

        try {
            conn = (HttpURLConnection)(new URL(url)).openConnection();

            conn.setRequestMethod("PUT");
            conn.setReadTimeout(3600*1000);

            conn.setRequestProperty("Content-Type", "application/octet-stream");

            File temp = new File(file_path);
            int length = (int)temp.length();
            conn.setFixedLengthStreamingMode(length);

            conn.setUseCaches (false);
            conn.setDoInput(true);
            conn.setDoOutput(true);

            conn.setRequestProperty("Authorization", "xxx");

            conn.connect();

            OutputStream out = new DataOutputStream(conn.getOutputStream());
            InputStream in = new FileInputStream(file_path);

            int bytesAvailable = in.available();

            int maxBufferSize = 1024 * 300;
            int totalSize = bytesAvailable;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];

            long bytesRead = in.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {    
                out.write(buffer, 0, bufferSize);

                bytesAvailable = in.available();

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = in.read(buffer, 0, bufferSize);
            }    

            out.flush();
            out.close();
            in.close();

            InputStream is = conn.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            rd.close();
            is.close();

        } catch (Exception e) {
            return false;

        } finally {

            if(conn != null) {
                conn.disconnect();
            }
        }

【问题讨论】:

  • 你试过把你的文件分成若干块然后上传
  • 感谢您的评论,但您的回答没有用,如果可能的话,请您提供示例代码吗?我也已经尝试过块模式。但没有幸运。

标签: android ios http file-upload upload


【解决方案1】:

您可以尝试HttpClient jar下载最新的HttpClient jar,将其添加到您的项目中,然后使用以下方法上传文件:

private void uploadFile(String filePath) throws ParseException, IOException {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);

FileBody filebodyVideo = new FileBody(new File(filePath));
StringBody title = new StringBody("Filename: " + filePath);
StringBody description = new StringBody("This is a video of the agent");
StringBody code = new StringBody(realtorCodeStr);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("filePath", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
reqEntity.addPart("code", code);
httppost.setEntity(reqEntity);

// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );

// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if

if (resEntity != null) {
  resEntity.consumeContent( );
} // end if

httpclient.getConnectionManager( ).shutdown( );
}

【讨论】:

  • 谢谢,我也可以用 HttpClient,jar 来使用上传进度状态吗?
  • 我使用了 cz.msebera.android.httpclient.client.HttpClient,但还是有问题。
  • 我可以知道您用于传输的预期文件大小是多少?如果它大于 250 Mb,我会建议你使用文件加速器,如文件催化剂或 amazon s3 等。对于 noraml senario,你可以使用 Retrofit / Loopj 库来轻松处理文件上传
  • 正好是 25M
猜你喜欢
  • 2012-04-23
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2013-07-04
  • 2011-03-24
  • 1970-01-01
  • 2013-08-10
  • 2016-09-17
相关资源
最近更新 更多