太多打开文件错误android

时间:2016-04-07 08:20:52

标签: android okhttp

我正在实施将文件上传到云服务的服务。 我使用OkHttp以块的形式上传大文件。 逻辑是这样的,我从服务器获取加载URL和头,然后将块上传到云,依此类推。 在大约200块之后我遇到这个错误的问题:

 W/System.err: java.net.ConnectException: failed to connect to piboxdev.blob.core.windows.net/40.118.73.216 (port 443) after 10000ms: connect failed: EMFILE (Too many open files)
 04-07 11:10:55.963 4945-5653/? W/StreamManager: Dropping non-bitmap icon from notification.
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at java.net.Socket.connect(Socket.java:882)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:190)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Connection.connectSocket(Connection.java:196)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Connection.connect(Connection.java:172)
 04-07 11:10:55.977 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:367)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Call.getResponse(Call.java:267)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at com.squareup.okhttp.Call.execute(Call.java:79)
 04-07 11:10:55.978 13347-15602/by.set.pibox W/System.err:     at by.set.pibox.upload.uploadService.HttpUploadTask.loadToAzure(HttpUploadTask.java:254)

以下是处理上传的代码:

   try {
        HashMap<String, String> headers = response.getHeaders();
        String url = response.getUrl();

        if (source==null)
            source = Okio.buffer(Okio.source(file));

        if (content==null)
        content = new ChunkFileRequestBody(source, size, contentType);

        Request.Builder builder = new Request.Builder();

        for (Map.Entry<String, String>entry : headers.entrySet()) {
          builder.addHeader(entry.getKey(), entry.getValue());
        }

        Request azureRequest =  builder.url(url).put(content).build();
        Response azureResponse = client.newCall(azureRequest).execute();
        if (azureResponse.code()/100==2 && shouldContinue) {
            Log.e(getClass().getSimpleName(), azureResponse.code() + " " + azureResponse.message());
            uploadedBodyBytes +=size;
            if (uploadedBodyBytes>=totalBodyBytes) {
                cleanUp();
                confirmUpload();
            }
            else {
                broadcastProgress(uploadedBodyBytes, totalBodyBytes);
                upload();
            }

这是自定义内容正文:

public class ChunkFileRequestBody extends RequestBody {

private final String contentType;
private long uploadSize;
private BufferedSource source;

public ChunkFileRequestBody(BufferedSource source, Long size, String contentType) {
    this.source = source;
    this.contentType = contentType;
    this.uploadSize=size;
}

@Override
public long contentLength() {
    return uploadSize;
}

@Override
public MediaType contentType() {
    return MediaType.parse(contentType);
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
        source.readFully(sink.buffer(), uploadSize);
        sink.flush();
}


}

这不是文件处理中的泄漏,因为我只打开一次文件。 内存使用情况似乎也很好。 看起来好像OkHttp无法正确关闭套接字。 我问图书馆的作者,但他们无法帮助解决这个问题。

有人有过类似的东西吗?请帮忙

1 个答案:

答案 0 :(得分:0)

您如何实例化您的http客户端?我只是通过不每次实例化一个新客户端来解决这个问题,而只是创建一个并重新使用它。