channel 을 처음 사용해 봤습니다.

clear 와 flip 을 적절히 사용하여 받아오는 데이터를 알 수 있었습니다.

flip을 사용하지 않을 경우 최초 셋팅된 128 Byte의 공간을 모두 저장 하게 되어 마지막에 0x00 이

기록 되어 버립니다.

서두가 없기에 글이 어떤 내용인지 알아먹기 어렵지만, 나름 저만 알아 먹도록 하겠습니다.

^.^

public long getFile(String strRemoteFile, String strLocalFile) {
        FtpInputStream fileInputStream = null;
        ByteBuffer byteBuf = null;
        FileChannel fileChannelOut = null;
        FileOutputStream fileOutputStream = null;
       
        int size = 0;
        byte[] byteBuff = new byte[128];
        long longTotalByte = 0;
       
        try {
             /* source FtpFile remote file */
            FtpFile file = new FtpFile(strRemoteFile, this.objFTP);
            /* open ftp input stream */
            fileInputStream = new FtpInputStream(file);
           
            fileOutputStream    = new FileOutputStream(strLocalFile);
            fileChannelOut         = fileOutputStream.getChannel();
                       
            byteBuf = ByteBuffer.allocate(byteBuff.length);
           
            while( 0 < ( size =  fileInputStream.read(byteBuff) ) ){
                byteBuf.clear();
                byteBuf.put(byteBuff, 0 , size);               
                byteBuf.flip();
                fileChannelOut.write(byteBuf);
                longTotalByte += size;
            }
           
        }
        catch (IOException e) {
            longTotalByte = 0;
        }
        finally {
            /* close ftp input stream
             * this must be always run */
            if (fileInputStream != null)
                try {
                    fileChannelOut.close();
                    fileOutputStream.close();
                    fileInputStream.close();                   
                }
                catch (IOException e) {
                    longTotalByte =  0;
                }
        }
        return longTotalByte;       
    }

Leave a Reply

Your email address will not be published. Required fields are marked *