Android 如何提高大尺寸文件的上传速度?

Android 如何提高大尺寸文件的上传速度?,android,ios,http,file-upload,upload,Android,Ios,Http,File Upload,Upload,我正在Android操作系统上开发文件上传应用程序 基本上我使用的是HttpURLConnection,这是必需的 大约相同的文件大小,IOS非常快,我使用AFN网络 但是Android太慢了,请告诉我我错过了什么 这是我使用的源代码 多谢各位 HttpURLConnection conn = null; try { conn = (HttpURLConnection)(new URL(url)).openConnection();

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

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

大约相同的文件大小,IOS非常快,我使用AFN网络

但是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();
            }
        }
您可以尝试下载最新的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,但仍然有问题。我可以知道您用于传输的预期文件大小是多少吗?如果大于250MB,我建议您使用文件加速器,如FileCatalyst或AmazonS3等。。对于noraml senario,您可以使用Reformation/Loopj库来轻松处理文件上载—实际上它是2500万