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文件。我能够读取可变块记录。但是,在写作时,如果我不添加额外的空间以匹配文件记录长度,则数据会变得混乱。如果我增加了额外的空间,它会消耗大量内存。我在这里错过了什么吗?我该如何解决此问题?..
我想您的问题是ftps2.setFileType(FTP.BINARY_FILE_TYPE);
。
Windows/Unix/Linux文件对"记录"一无所知,每个文件只是字节流。使用流的文本文件时,可能包含线结束字符(ASCII中的x0A
,x0D
或x0D0A
(。这些可以解释为记录的末尾和新的开始,因此,当它们在文本模式下遇到其中一个时,大多数FTP-Tool都会在Z/OS方面启动新记录(and(VICE VERSA在从 z/os传输时启动新记录时添加一个。
使用二进制文件有些不同,因为x0D
和x0A
不会以任何特殊的方式处理,但仅是两个字节值。
因此,要获得您想要的东西,您拥有以下这些问题:
- 使用文本模式传输文件,但这很可能会导致某种代码转换。如果可能的话,您可以完全不进行转换。
- 将二进制文件中的文件传输到某些FB数据集,并编写一个工具,以将连续的副流划分为正确的线终止字符,然后将结果记录写入VB-Dataset。