Android Retrofit 2文件上传无效

时间:2016-11-14 12:14:37

标签: android file-upload retrofit2 okhttp

我想开发一个应用程序,它可以将任何类型的文件上传到服务器。我有源代码。但它只适用于图像。并且只上传低于2 MB大小的图像文件。我想上传所有类型的文件。请帮我。我的代码如下所示。

MainActivity.java

public class MainActivity extends AppCompatActivity {
Service service;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button) findViewById(R.id.btn_upload);

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    // Change base URL to your upload server URL.
    service = new Retrofit.Builder().baseUrl("http://192.168.1.4/work/filesharing/").client(client).build().create(Service.class);

    if (btn != null) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String filePath1 = "/storage/emulated/0/gt.png";

                File file = new File(filePath1);

                RequestBody reqFile = RequestBody.create(MediaType.parse("*/*"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", file.getName(), reqFile);
                RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");



                retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
                req.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        t.printStackTrace();

                        Log.d("failure", "message = " + t.getMessage());
                        Log.d("failure", "cause = " + t.getCause());
                    }
                });

            }
        });
    }
}

}

将zip文件上传到2MB以上时会出现错误。

11-14 17:09:14.369 13895-13960/app3.acs.com.file_upload2 D/OpenGLRenderer: Enabling debug mode 0
11-14 17:09:16.068 13895-13993/app3.acs.com.file_upload2 D/OkHttp: --> POST http://192.168.1.4/work/filesharing/6.php HTTP/1.1
11-14 17:09:16.068 13895-13993/app3.acs.com.file_upload2 D/OkHttp: Content-Type: multipart/form-data; boundary=b2c1d03e-f7a5-4e3f-89e1-d1c20358f7dc
11-14 17:09:16.070 13895-13993/app3.acs.com.file_upload2 D/OkHttp: Content-Length: 20862337
11-14 17:09:16.934 13895-13919/app3.acs.com.file_upload2 W/art: Suspending all threads took: 30.363ms
11-14 17:09:16.935 13895-13993/app3.acs.com.file_upload2 I/art: Alloc partial concurrent mark sweep GC freed 20318(40MB) AllocSpace objects, 0(0B) LOS objects, 35% free, 28MB/44MB, paused 644us total 49.776ms
11-14 17:09:18.272 13895-13993/app3.acs.com.file_upload2 I/art: Alloc sticky concurrent mark sweep GC freed 8(256B) AllocSpace objects, 0(0B) LOS objects, 10% free, 68MB/76MB, paused 1.012ms total 6.892ms
11-14 17:09:18.293 13895-13993/app3.acs.com.file_upload2 I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 18% free, 68MB/84MB, paused 1.310ms total 18.796ms
11-14 17:09:18.330 13895-13993/app3.acs.com.file_upload2 I/art: Alloc concurrent mark sweep GC freed 6(12KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 68MB/84MB, paused 627us total 34.490ms
11-14 17:09:18.332 13895-13993/app3.acs.com.file_upload2 I/art: Forcing collection of SoftReferences for 36MB allocation
11-14 17:09:18.371 13895-13993/app3.acs.com.file_upload2 I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 18% free, 68MB/84MB, paused 569us total 38.975ms
11-14 17:09:18.373 13895-13993/app3.acs.com.file_upload2 E/art: Throwing OutOfMemoryError "Failed to allocate a 38772598 byte allocation with 16777120 free bytes and 27MB until OOM"
11-14 17:09:18.381 13895-13993/app3.acs.com.file_upload2 E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
                                                                       Process: app3.acs.com.file_upload2, PID: 13895
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 38772598 byte allocation with 16777120 free bytes and 27MB until OOM
                                                                           at java.lang.String.<init>(String.java:332)
                                                                           at java.lang.String.<init>(String.java:371)
                                                                           at okio.Buffer.readString(Buffer.java:579)
                                                                           at okio.Buffer.readString(Buffer.java:562)
                                                                           at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:195)
                                                                           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
                                                                           at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                           at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                           at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                           at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                           at java.lang.Thread.run(Thread.java:818)
11-14 17:09:20.069 13895-13993/app3.acs.com.file_upload2 I/Process: Sending signal. PID: 13895 SIG: 9

6 个答案:

答案 0 :(得分:0)

请降低日志记录级别或将其全部删除

通过此链接https://github.com/square/retrofit/issues/540

查看JakeWharton的回复

&#34;通过调用.setLogLevel(RestAdapter.LogLevel.FULL),您强制Retrofit将整个请求主体缓冲到内存中,以便它可以记录。这就是堆栈跟踪中对readBodyToBytesIfNecessary的调用。

只有在调试时才能启用这样的日志记录。&#34;

答案 1 :(得分:0)

与此问题相关https://github.com/square/retrofit/issues/540 。您可以禁用拦截器Level.BODY。 它可以适用于所有类型的文件,你尝试过使用其他类型的文件吗?

答案 2 :(得分:0)

你也可以在这里引用https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server它将有助于上传文件

答案 3 :(得分:0)

我得到了答案。这是xampp服务器中的问题。现在我可以发送大文件了。我从这个链接得到了答案 How to Increase Import Size Limit in phpMyAdmin using Xampp server?

但速度很慢。无论如何它都能正常工作。

答案 4 :(得分:0)

上传大型图像或视频时,我遇到了同样的问题。主要问题是您在HttpLoggingInterceptor中使用主体级别,而记录器正在logcat中打印文件,从而触发java.lang.OutOfMemoryError异常。

如果您不想禁用主体级别的登录,则可以添加此代码,以避免登录生成大文件内容。我创建了自己的HttpLoggingInterceptor并添加了if条件,以在content-length> 10Ks的情况下跳过主体登录。

HttpLoggingInterceptor是最终类,因此您无法覆盖它。您可以复制HttpLoggingInterceptor文件并替换此原始代码:

if (!logBody || !hasRequestBody) {
            logger.log("--> END " + request.method());
        } else if (bodyEncoded(request.headers())) {
            logger.log("--> END " + request.method() + " (encoded body omitted)");
        } else {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);
            //[...]

使用以下代码:

if (!logBody || !hasRequestBody) {
            logger.log("--> END " + request.method());
        } else if (bodyEncoded(request.headers())) {
            logger.log("--> END " + request.method() + " (encoded body omitted)");
        } else if (requestBody.contentLength()>1024 * 10) { //>10Kb
            logger.log("--> END " + request.method() + " (contentLength > 10Kb --> omitted)");
        } else {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);
            //[...]

我希望这可以帮助任何人。对不起,我的英语;)。

答案 5 :(得分:0)

我遇到了同样的问题,但是我发现库中没有问题。我的问题已解决。 其服务器端问题。 您可能需要检查以下几点来解决它:-

  1. 检查如何将冗长的文件上载到服务器。
  2. 从服务器角度检查服务器连接超时。
  3. 确保还必须更改Apache服务器上的文件大小。 (就我而言,Apache服务器的php文件大小为5MB)