Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了太多打开文件错误android大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在实施一项将文件上传到云服务的服务.
我正在使用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().getSimpl@R_874_8371@(),azureResponse.code() + " " + azureResponse.message());
            uploadedBodyBytes +=size;
            if (uploadedBodyBytes>=@R_524_10586@lBodyBytes) {
                cleanUp();
                confirmUpload();
            }
            else {
                broadcastProgress(uploadedBodyBytes,@R_524_10586@lBodyBytes);
                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无法正确关闭套接字.
我问图书馆的作者,但他们无法帮助解决这个问题.

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

解决方法

你是如何实例化你的http客户端的?我刚刚解决了这个问题,不是每次都实例化一个新的客户端,而只是创建一个并重用它.

大佬总结

以上是大佬教程为你收集整理的太多打开文件错误android全部内容,希望文章能够帮你解决太多打开文件错误android所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。