• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

com.enterprisedt.net.ftp Ftp corrupts the tar.gz file while copying the file.

 
Andy Saxena
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I am in a situation where my ftp code copies the tar.gz file from the remote location. But that file is somehow corrupted. Mu unzip code in java fails, also, when I manually try to unzip the file in windows using WinRAR, it also says that the package is corrupted. Whereas the file in server is perfect.

I tried using Apache Commons Net FTP jar as well. But there also the same problem.

Below is my FTP code :

FileTransferClient ftp = null;
String fileName = "";
// create client
logger.info("\nCreating FTP client");
ftp = new FileTransferClient();
ftp.setRemoteHost(serverName);
ftp.setUserName(userName);
ftp.setPassword(password);
try {
ftp.connect();
} catch (Exception e) {
System.out.println("ERROR: ");
System.out.println("Please Check the Following :");
System.out.println("-> Server Name or IP Address");
System.out.println("-> User Name");
System.out.println("-> Password for <User Name> Account");
System.out.println("ERROR: Could not connect to server. DBX will now exit..");
System.exit(0);
}

logger.info("Connected and logged in to server " + serverName);
int index = remoteDir.lastIndexOf("/");
String parentRemoteDir = remoteDir.substring(0, index);
fileName = remoteDir.substring(index + 1);
ftp.changeDirectory(parentRemoteDir);

ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);

ftp.setContentType(FTPTransferType.BINARY);
FTPFile[] files = ftp.directoryList(".");
if (loadMYSQLArchive) {

// get MYSQL Archives
boolean localDbxArchiveExists = (new File(localDir)).exists();
if (!localDbxArchiveExists) {
boolean createLocalDbxArchive = (new File(localDir)).mkdir();
if (createLocalDbxArchive) {
}
} else {
boolean createLocalDbxArchive = (new File(localDir)).mkdir();

if (createLocalDbxArchive) {
logger.info("Local directory created at \"" + localDir + "\"");
}
}
int dwnldedFiles = 0;
for (int i = 0; i < files.length; i++) {
if (!files[i].isDir() && files[i].getName().equalsIgnoreCase(fileName)) {
logger.info("Downloading file from \"" + files[i].getName() + " To location \"" + localDir + File.separator
+ files[i].getName() + "\"");
FileTransferInputStream downloadedStream = ftp.downloadStream(files[i].getName());
final FileOutputStream out = new FileOutputStream(localDir + File.separator + files[i].getName());
byte[] bytes = new byte[256];
int len;
while ((len = downloadedStream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
downloadedStream.close();
out.close();

// ftp.downloadFile(localDir + File.separator + files[i].getName(), files[i].getName());
dwnldedFiles++;
} else {
logger.info("Skipping directory " + files[i].getName());
}
}
if (dwnldedFiles == 0) {
throw new Exception("DB archive not found, please check the archive path.");
}

}
ftp.disconnect();

Please help me with this issue.

Thanks
~Andy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic