2 gundumw100 gundumw100 于 2015.07.12 15:26 提问

volley上传图片报read failed: EBADF (Bad file number)

手机系统android4.4
想上传图片到服务器
基本代码为:

            public void addPart(final String key, final String fileName, final InputStream fis, String type, final boolean isLast)
{
                    writeFirstBoundaryIfNeeds();
                    try
                    {
                    type = "Content-Type: " + type + "\r\n";
                    out.write(("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + fileName + "\"\r\n").getBytes());
                    out.write(type.getBytes());
                    out.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
                    final byte[] tmp = new byte[4096];
            int l = 0;
            //java.io.IOException: read failed: EBADF (Bad file number)
            // Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
            while ((l = fis.read(tmp)) > 0)
            {
                out.write(tmp, 0, l);
            }
            if (!isLast)
                out.write(("\r\n--" + boundary + "\r\n").getBytes());
            else
            {
                writeLastBoundaryIfNeeds();
            }
            out.flush();
            out.close();
            fis.close();
        }
        catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

报错点位于while ((l = fis.read(tmp)) > 0)这句代码。
完整的报错:
07-12 15:23:29.778: W/System.err(1267): java.io.IOException: read failed: EBADF (Bad file number)
07-12 15:23:29.779: W/System.err(1267): at libcore.io.IoBridge.read(IoBridge.java:435)
07-12 15:23:29.779: W/System.err(1267): at java.io.FileInputStream.read(FileInputStream.java:179)
07-12 15:23:29.780: W/System.err(1267): at java.io.InputStream.read(InputStream.java:162)
07-12 15:23:29.780: W/System.err(1267): at com.yirui.youbao.net.MultipartEntity.addPart(MultipartEntity.java:114)
07-12 15:23:29.781: W/System.err(1267): at com.yirui.youbao.net.MultipartEntity.addPart(MultipartEntity.java:98)
07-12 15:23:29.781: W/System.err(1267): at com.yirui.youbao.net.MultipartRequestParams.getEntity(MultipartRequestParams.java:86)
07-12 15:23:29.782: W/System.err(1267): at com.yirui.youbao.net.MultipartRequest.getBody(MultipartRequest.java:59)
07-12 15:23:29.782: W/System.err(1267): at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:223)
07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
07-12 15:23:29.784: W/System.err(1267): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
07-12 15:23:29.785: W/System.err(1267): Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
07-12 15:23:29.786: W/System.err(1267): at libcore.io.Posix.readBytes(Native Method)
07-12 15:23:29.787: W/System.err(1267): at libcore.io.Posix.read(Posix.java:128)
07-12 15:23:29.788: W/System.err(1267): at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
07-12 15:23:29.789: W/System.err(1267): at libcore.io.IoBridge.read(IoBridge.java:425)
07-12 15:23:29.789: W/System.err(1267): ... 11 more

很奇怪,这是什么引起的?

1个回答

devmiao
devmiao   Ds   Rxr 2015.07.15 23:27
Csdn user default icon
上传中...
上传图片
插入图片