java.lang.IndexOutOfBoundsException at java.io.FileOutputStream.writeBytes(Native Method)

本文探讨了在Java中进行文件流操作时遇到的IndexOutOfBoundsException问题,并提供了可能的原因及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 测试 DDN wos 的时候出现错误: 

 available : 3212
/usr/lk/data/linkapp/ddn_1440639847758_temp
1024
1024
java.lang.IndexOutOfBoundsException
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.java:260)
    at com.lk.ddn.DDNRestUtil.getData(DDNRestUtil.java:439)
    at com.lk.ddn.DDNRestUtil.main(DDNRestUtil.java:88)

 

 

相关代码是:

    public static byte[] getData(String oid) {
        if (StringUtils.isEmpty(oid)) {
            return null;
        }
        int nFileLength = -1;
        String meta = "";
        byte[] fileData = null;
        try {
            String httpurl = "http://"+host+"/cmd/get";
            httpurl = "http://localhost/disk/myFileList.action";
            httpurl = "http://localhost/register.jsp";
            URL url = new URL(httpurl);
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("GET");
//            httpConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 " +
//                    "(KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36");
            httpConnection.setRequestProperty("content-type", "application/octet-stream");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            
            int responseCode = httpConnection.getResponseCode();
            if (responseCode >= 400) {
                processErrorCode(responseCode);
                return null; // -2 represent access is error
            }
            
            if (responseCode == HttpURLConnection.HTTP_OK) {
                if (1==1) {
                    
                    
                    InputStream inputStream = null;
                    try {
//                        inputStream = httpConnection.getInputStream();
//                        int available = inputStream.available();
//                        System.out.println(" available : " + available);
//                        fileData = new byte[available];
//                        int read = inputStream.read(fileData);
//                        System.out.println(" read : " + read);
//                        int read = 0;
//                        while ((read = inputStream.read(fileData)) != -1) {
//                            
//                        }
                        
//                        String contentEncoding = httpConnection.getContentEncoding();
//                        System.out.println(" contentEncoding : " + contentEncoding);
//                        
//                        inputStream = httpConnection.getInputStream();
//                        int available = inputStream.available();
//                        fileData = new byte[nFileLength];
//                        int read = inputStream.read(fileData);
//                        System.out.println(" read : " + read);
//                        while ((read = inputStream.read(fileData)) != -1) {
//                            
//                        }
                        
                        inputStream = httpConnection.getInputStream();
                        int available = inputStream.available();
                        System.out.println(" available : " + available);
                        
                        int offset = 0;
                        int reads;
                        int onesize = available>1024?1024:available;
//                        fileData = new byte[nFileLength];
//                        fis.reset();
//                        while ((reads = inputStream.read(fileData,offset,onesize)) != -1) {
//                            //System.out.println(new String(b));
//                            offset += reads;
//                            if (onesize >= available - offset) {
//                                onesize = available - offset;
//                                if (onesize <= 0) {
//                                    break;
//                                }
//                            }
//                        }
//                        while ((reads = inputStream.read(fileData)) != -1) {
//                            //System.out.println(new String(b));
//                            offset += reads;
//                        }
                        

                        
                        String downlowdPath = getTempFileDownlowdPath();
                        System.out.println(downlowdPath);
                        FileOutputStream fos = new FileOutputStream(downlowdPath);
                        int read = 0;
                        
                        int off = 0;
                        available = inputStream.available();
                        byte[] b = new byte[1024];
                        while ((read = inputStream.read(b))!= -1) {
                            System.out.println(read);
                            fos.write(b,off,read);
                            off += read;
                        }
                        if (b != null) {
//                            fos.write(b);
                        }
                        System.out.println("readed : " + read);
                        inputStream.close();
                        fos.flush();
                        fos.close();
//                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    }

                }
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileData;
        
    }

 

为什么总是不行呢???

 

这样的简单流操作、、

 

posted on 2015-08-27 09:49 CanntBelieve 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyAway2013/p/4762391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值