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