Reputation: 37
I'm new to this kind of work so please help me. I send a xlsx file (a excel file) to a server, there were no error in my code and either in the ftp server, I use filezilla in xampp. I search in google and say that it must be store in a zip file but the zip file is also corrupted. Here is my code
FTPClient upload= new FTPClient();
File firstLocalFile = new File("C:/Users/user/desktop/sample.xlsx");
InputStream inputStream = new FileInputStream(firstLocalFile);
try {
upload.connect("localhost");
upload.login("user", "pass");
upload.enterLocalPassiveMode();
upload.storeFile("sample.zip", inputStream);
} finally {
try {
upload.logout();
upload.disconnect();
} catch (Exception e) {
}
}
Any solution for my problem?
Upvotes: 0
Views: 1545
Reputation: 512
Try the following
public void fetchFile() throws Exception {
final FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(this.serverDetails.getHostName(), this.serverDetails.getPort());
if (!ftpClient.login(this.serverDetails.getUserName(), this.serverDetails.getPassword())) {
throw new IOException("cannot login to server (server reply: " + ftpClient.getReplyCode());
}
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
throw new IOException("Exception in connecting to FTP Server");
}
final FTPFile ftpFile = ftpClient.mlistFile(this.serverDetails.getPath());
if (ftpFile.isFile()) {
final InputStream is = ftpClient.retrieveFileStream(this.serverDetails.getPath());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
copyFromRemote(is, ftpFile.getSize());
} else {
throw new IOException("Remote file Doesnot Exist");
}
} catch (final Exception e) {
throw e;
}
finally {
ftpClient.logout();
ftpClient.disconnect();
}
}
Upvotes: -2
Reputation: 1947
You need to set the file type using -
upload.setFileType(FTPClient.BINARY_FILE_TYPE);
Also, add try and catch around upload.storeFile
to make sure its storing the file.
Upvotes: 7