17

I use retrofit for uploading image to server, but have a strange issue

api method declaration:

@POST("/uploadImage")
@Multipart
Result<UploadImageResponse> uploadImage(@Part("image") TypedByteArray image);

rest adapter user custom client:

OkClient client = new OkClient(getUnsafeOkHttpClient());

where

private OkHttpClient getUnsafeOkHttpClient() {

    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts,
                new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext
                .getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

exception log:

D/Retrofit: javax.net.ssl.SSLException: Write error: ssl=0x7f70604080: I/O error during system call, Broken pipe
                                                                   at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
                                                                   at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:771)
                                                                   at okio.Okio$1.write(Okio.java:80)
                                                                   at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
                                                                   at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
                                                                   at okio.RealBufferedSink.write(RealBufferedSink.java:46)
                                                                   at com.squareup.okhttp.internal.http.Http1xStream$FixedLengthSink.write(Http1xStream.java:288)
                                                                   at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
                                                                   at okio.RealBufferedSink$1.write(RealBufferedSink.java:198)
                                                                   at java.io.OutputStream.write(OutputStream.java:82)
                                                                   at retrofit.mime.TypedByteArray.writeTo(TypedByteArray.java:66)
                                                                   at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:68)
                                                                   at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
                                                                   at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
                                                                   at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
                                                                   at java.lang.reflect.Proxy.invoke(Proxy.java:393)
                                                                   at $Proxy3.uploadImage(Unknown Source)
Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
  • 1
    Duplicate of [SSL Broken Pipe](http://stackoverflow.com/questions/8053824/ssl-broken-pipe?rq=1) – user207421 Jun 27 '16 at 08:19
  • 3
    It basically means that a server side closed the connection (abruptly). Go to the server and check the log files. – Ales Teska Jul 03 '16 at 14:44
  • Please check below url. http://stackoverflow.com/questions/8053824/ssl-broken-pipe – Chirag Arora Jul 04 '16 at 07:11
  • Possible duplicate of [javax.net.ssl.SSLException: Read error: ssl=0x9524b800: I/O error during system call, Connection reset by peer](http://stackoverflow.com/questions/30538640/javax-net-ssl-sslexception-read-error-ssl-0x9524b800-i-o-error-during-system) – Alex Klimashevsky Jul 07 '16 at 09:04
  • It's NOT a duplicate, this is specifically about Retrofit library, that other one being linked is not. – Fran Marzoa Jan 22 '21 at 11:31

1 Answers1

19

This exception is the result of file limit on server side

Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
  • what kind of limit? – Yoav Feuerstein Aug 21 '19 at 13:48
  • 2
    @YoavFeuerstein as I remember apatche has max file upload size. If you try to upload bigger file you will have this exception – Alex Klimashevsky Sep 12 '19 at 07:45
  • 1
    For others looking, I was having the exact same problem, the issue was that the `Content-Length` header wasn't set correctly, in my case was while using retrofit from Android, I documented the solution in a post [here](https://gutier.io/post/android-upload-file-to-aws-s3-bucket-with-retrofit2/) – zzantares Mar 24 '20 at 04:56
  • Then how to solve it Sir. – Ganesan J Mar 02 '23 at 10:08
  • @GanesanJ as I remember there was a configuration file at apatche. so you can do it via the next commands: `sudo vim /var/www/html/.htaccess` to edit the config file ``` LimitRequestBody 5242880 ``` change to a max size you would like to support – Alex Klimashevsky May 14 '23 at 21:11