2017-04-19 59 views
0
package base; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.PrintWriter; 
import org.apache.commons.net.PrintCommandListener; 
import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPConnectionClosedException; 
import org.apache.commons.net.ftp.FTPReply; 
import org.apache.commons.net.ftp.FTPSClient; 
import com.ibm.jzos.ZFile; 

public class FTPSVB { 

public static void main(String[] args) { 
    BufferedInputStream binp=null; 
    BufferedOutputStream bout=null; 

    String server, username, password, fileTgt, fileSrc; 
    String protocol = "TLS"; // SSL/TLS 
    FTPSClient ftps = new FTPSClient(protocol); 
    FTPSClient ftps2 = new FTPSClient(protocol); 
    server="***"; 
    username="***"; 
    password="***"; 
    fileSrc="ABC00T.SMP.SAVE.ULRL"; 
    fileTgt="ABC00T.SMP.SAVE.OUT.ULRL"; 
    try 
    { 
     int reply; 
     ftps.connect(server); 
     ftps2.connect(server); 
     reply = ftps.getReplyCode(); 
     reply = ftps2.getReplyCode(); 
    } 
    try 
    { 
     ftps.setBufferSize(200); 
     ftps.setFileType(FTP.BINARY_FILE_TYPE); 
     if (!ftps.login(username, password)) 
     { 
      ftps.logout(); 
      System.out.println("ERROR.."); 
      System.exit(-1); 
     } 
     ftps.execPBSZ(0); 
     ftps.execPROT("P"); 

     ftps.enterLocalPassiveMode(); 
     ftps.setAutodetectUTF8(true); 
     ftps.site("QUOTE RDW"); 
     ftps2.setBufferSize(200); 
     ftps2.setFileType(FTP.BINARY_FILE_TYPE);  
     ftps2.site("QUOTE RDW"); 
     ftps2.site("QUOTE recfm=VB lrecl=106 blksize=27998"); 
     if (!ftps2.login(username, password)) 
     { 
      ftps2.logout(); 
      System.out.println("ERROR.."); 
      System.exit(-1); 
     } 
     ftps2.execPBSZ(0); 
     ftps2.execPROT("P"); 
     ftps2.enterLocalPassiveMode(); 
     ftps2.setAutodetectUTF8(true); 
     binp=new BufferedInputStream(ftps.retrieveFileStream(fileSrc)); 
     bout=new BufferedOutputStream(ftps2.storeFileStream(fileTgt)); 
     final byte []bufLen= new byte[4]; 
     int readLen=binp.read(bufLen, 0, 4);// Read len 
     int recCounter=1; 
     while(readLen!=-1){ 
     ByteArrayInputStream ba2=new ByteArrayInputStream (bufLen,0,4); 
      int z=ba2.read(); 
      int reclen=0; 
      int li=0; 
      while(z!=-1){ 
       if(li==0) 
        reclen+=z*256; 
       else if(li==1) 
        reclen+=z; 
       li++; 
       z=ba2.read(); 
      } 
      ba2.close(); 
      reclen-=4; 
      byte []buf=new byte[reclen]; 
      readLen=binp.read(buf, 0, reclen); 
      boolean isEOF=false; 
      while(readLen<reclen) { 
       int nextLen=binp.read(buf, readLen, reclen-readLen); 
       if(nextLen==-1){// End of file is reached. 
        isEOF=true; 
        break; 
       } 
       readLen=readLen+nextLen; 
      } 
      String a=new String(buf, ZFile.DEFAULT_EBCDIC_CODE_PAGE); 
      StringBuilder str=new StringBuilder(a); 
      //str.append(System.getProperty("line.separator")); 
      System.out.println(""+str); 
     //appending extra space for record till its length matches file record length 
      if(str.length()<102) { 
       for (int i = str.length(); i < 102; i++) { 
        str.append(" "); 
       } 
      } 
      byte []outBytes=new byte[102]; 
     outBytes=str.toString().getBytes(ZFile.DEFAULT_EBCDIC_CODE_PAGE); 
      bout.write(outBytes); 
      if(isEOF){ 
       break; 
      } 
      readLen=binp.read(bufLen, 0, 4);// Read length- RDW 
      recCounter++; 
     } 
     bout.flush(); 
     bout.close(); 
     binp.close(); 
     ftps.completePendingCommand(); 
     ftps2.completePendingCommand(); 
     ftps.logout(); 
    } 
    catch (FTPConnectionClosedException e) 
    { 
     System.err.println("Server closed connection."); 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (ftps.isConnected()) 
     { 
      try 
      { 
       ftps.disconnect(); 
      } 
      catch (IOException f) 
      { 
       // do nothing 
       f.printStackTrace(); 
      } 
     } 
    } 
    } 
} 

我正在使用上面的代碼讀取和寫入VB文件。我可以讀取變量塊記錄。但是如果我不附加額外的空間來匹配文件記錄長度,那麼寫入時數據就會變得混亂。如果我增加額外的空間,它會消耗大量的內存。我在這裏錯過了什麼嗎?我如何解決這個問題?使用FTP/FTPS將數據寫入VB(可變塊)文件

+0

小事情......我注意到ftps2,你在登錄前發送了「SITE QUOTE」命令 - 我想你可能需要先登錄,然後發送你的SITE命令。從你的描述來看,這聽起來似乎並不真正獲得VB格式的記錄......如果你有一種方法用TSO登錄到大型機,請檢查文件屬性以確保FTP分配了你認爲應該有的VB數據集。我總是喜歡在交互式會話中手動嘗試這些事情,直到我確信在編寫代碼之前一切都按預期工作。 –

回答

0

我想你的問題是ftps2.setFileType(FTP.BINARY_FILE_TYPE);

Windows/Unix/Linux文件對「記錄」一無所知,每個文件只是一個字節流。處理文本文件時,該文件流可能包含換行符(x0A,x0Dx0D0A,ASCII)。這些可能會被解釋爲記錄結束並開始新記錄,因此大多數FTP工具會在z/OS端啓動新記錄,當它們遇到文本模式(反之亦然時添加一個從 z/OS傳輸時開始新記錄)。

與二進制文件的東西有點不同,因爲x0Dx0A不以任何特殊的方式處理,但只是其中的兩個字節值。

因此,爲了得到你想要的,你有這些posibilities:

  1. 轉移使用文本模式的文件,但是這很可能會導致某種形式的代碼頁轉換正在做。如果可能的話,你可以配置一個自定義轉換表,根本不做任何轉換。
  2. 將二進制文件傳輸到某個FB數據集,然後編寫一個工具將連續字節流分割爲正確的行終止字符,並將結果記錄寫入VB數據集。