gpt4 book ai didi

java - 将输入流传递给其他线程时读取失败

转载 作者:行者123 更新时间:2023-11-30 10:59:51 25 4
gpt4 key购买 nike

我正在尝试将 InputStream 写入 MultiPartEntity 但我一直在点击:

08-01 17:25:13.523    2737-2787/com.[project].[package].test E/Volley﹕ [12495] BasicNetwork.performRequest: Unexpected response code 401 for https://api.[project].com/
08-01 17:25:13.603 2737-2737/com.[project].[package].test W/System.err﹕ com.android.volley.NoConnectionError: java.io.IOException: read failed: EBADF (Bad file number)
08-01 17:25:13.603 2737-2737/com.[project].[package].test W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
08-01 17:25:13.603 2737-2737/com.[project].[package].test W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
08-01 17:25:13.604 2737-2737/com.[project].[package].test W/System.err﹕ Caused by: java.io.IOException: read failed: EBADF (Bad file number)
08-01 17:25:13.605 2737-2737/com.[project].[package].test W/System.err﹕ at libcore.io.IoBridge.read(IoBridge.java:482)
08-01 17:25:13.605 2737-2737/com.[project].[package].test W/System.err﹕ at java.io.FileInputStream.read(FileInputStream.java:177)
08-01 17:25:13.605 2737-2737/com.[project].[package].test W/System.err﹕ at java.io.InputStream.read(InputStream.java:162)
08-01 17:25:13.605 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.entity.mime.content.InputStreamBody.writeTo(InputStreamBody.java:91)
08-01 17:25:13.605 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:150)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:173)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:97)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.execchain.RequestEntityProxy.writeTo(RequestEntityProxy.java:116)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:155)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:149)
08-01 17:25:13.606 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:242)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:260)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:178)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
08-01 17:25:13.607 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
08-01 17:25:13.608 2737-2737/com.[project].[package].test W/System.err﹕ at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
08-01 17:25:13.608 2737-2737/com.[project].[package].test W/System.err﹕ at com.android.volley.toolbox.HttpClientStack.performRequest(HttpClientStack.java:87)
08-01 17:25:13.608 2737-2737/com.[project].[package].test W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
08-01 17:25:13.608 2737-2737/com.[project].[package].test W/System.err﹕ ... 1 more
08-01 17:25:13.609 2737-2737/com.[project].[package].test W/System.err﹕ Caused by: android.system.ErrnoException: read failed: EBADF (Bad file number)
08-01 17:25:13.610 2737-2737/com.[project].[package].test W/System.err﹕ at libcore.io.Posix.readBytes(Native Method)
08-01 17:25:13.610 2737-2737/com.[project].[package].test W/System.err﹕ at libcore.io.Posix.read(Posix.java:165)
08-01 17:25:13.610 2737-2737/com.[project].[package].test W/System.err﹕ at libcore.io.BlockGuardOs.read(BlockGuardOs.java:230)
08-01 17:25:13.610 2737-2737/com.[project].[package].test W/System.err﹕ at libcore.io.IoBridge.read(IoBridge.java:472)
08-01 17:25:13.610 2737-2737/com.[project].[package].test W/System.err﹕ ... 21 more

流是从 AssetFileDescriptor.createInputStream() 返回的 FileInputStream

在这里我得到了 AssetFileDescriptor

AssetFileDescriptor file = getContext().getContentResolver().openAssetFileDescriptor(person,
"r");

然后将其添加到多部分:

multipartRequest.addFileBody("file", file.createInputStream());

它正在使用 MultipartEntityBuilder:

public void addFileBody(String name, InputStream in){
multipartEntityBuilder.addBinaryBody(name, in);
}

我正在使用 HttpURLConnection并在这里写正文:

这(以及整个连接)发生在不同的线程上

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
throws IOException, AuthFailureError {
if(request.getEntity() != null){
connection.setDoOutput(true);
connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
connection.setChunkedStreamingMode(0);
OutputStream out = connection.getOutputStream();
request.getEntity().writeTo(out);
out.flush();
out.close();
} else {

我尝试用谷歌搜索它,但我所能发现的是,InputStream 很可能已关闭,但我并没有在任何地方关闭它。

编辑: 似乎问题是我试图从一个单独的线程发出请求,因为当它发生在同一个线程上时一切正常。

长话短说:线程 A 获取文件流并创建请求对象,将流添加到对象。线程 B 启动并获取所述对象,打开连接,并尝试读取流/写入连接,此时发生读取错误。

最佳答案

[[ 结果是她/他正在读取 Assets 并将其发送到远程,所以这个答案并没有真正帮助。我暂时将其保留为占位符。 ]]

System.err﹕ Caused by: android.system.ErrnoException: read failed: EBADF (Bad file number)

正如@EJP 提到的,这试图告诉您当后台线程开始从流中读取时,它已经关闭。

因为这似乎是一个网络客户端,我怀疑您正在尝试从 GET 或 POST 响应中读取文件,并且响应容器已关闭并在线程开始读取它时关闭。

有几种方法可以解决这个问题:

  • 发出请求的线程可以读取文件本身,然后将缓冲区中的字节传递给后台线程进行处理。

  • 发出请求的线程可以读入文件并将其保存在磁盘上的临时文件中,然后将临时文件名交给后台线程。

但无论如何,处理网络请求的线程必须在网络请求容器关闭之前完成请求的 IO。

关于java - 将输入流传递给其他线程时读取失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31767741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com