How to copy a file in FTP server using FTPClient in Java? - java

I have a CSV file, and I need to copy it and rename it in the same path.
I tried this after the FTP login:
InputStream inputStream = ftpClient.retrieveFileStream(cvs_name +".csv");
ftpClient.storeFile(cvs_name2 + ".csv",inputStream);
But when I verify the file on the server, it's empty. How can I copy a file and rename it?

I believe your code cannot work. You cannot download and upload a file over a single FTP connection at the same time.
You have two options:
Download the file completely first (to a temporary file or to a memory).
The accepted answer to How to copy a file on the ftp server to a directory on the same server in java? shows the "to memory" solution. Note the outputStream.toByteArray() call.
Open two connections (two instances of the FTPClient) and copy the file between the instances.
InputStream inputStream = ftpClient1.retrieveFileStream(cvs_name + ".csv");
ftpClient2.storeFile(cvs_name2 + ".csv", inputStream);

Related

Upload a file to an SFTP server using PDFBox save method without storing the file to the local system?

I'm trying to save the edited PDF which I fetched from the remote server back to its location without having it downloaded/stored on the local machine. I'm using JSch SFTP method to get the input PDF file from the SFTP server using
x = new BufferedInputStream(channelSftp.get("example.pdf"));
PDDocument document = PDDocument.load(x);
and after doing some edits using PDFbox, I'm trying to save it using:
documents.save(new File("ftp/path/location"));
I am not able to because I know it only works for your local directory only. Also I can see that document.save accept OutputStream` parameter, but I do not know how to use it here.
I don't have any problems with taking input using stream reader.
All I need is to save that edited PDF back to its location (possibly replace) without having to download it on my local system.
Use the ChannelSftp.put overload that returns OutputStream:
try (OutputStream out = channelSftp.put("example.pdf")) {
documents.save(out);
}

change exchange content in camel

I am trying to implement a scenario using camel which is as follows:-
Get File From JMS queue.
Store the file and crc value of the file in a directory
Move the file and crc file to the SFTP server and once it is successfull
delete the files from the directory.
My route is as follows
from("jms:queue")
.to(save the file)
.process(since I have the content of the file in exchange so generating crc)
.to(file system save the crc file)
.to(Push both the files to the sftp server);
But the file that is getting uploaded to the sftp server is only crc file as it was present in the exchange.
How should I solve this Situation?
If anyone of you have come across to this problem Please Guide me.
Thanks
I think you should use a wire tap to copy your exchange to a different route and have that new route save the file and sftp it to the correct location.
from("jms:queue")
.wireTap("direct:save-file")
.process(since I have the content of the file in exchange so generating crc)
.to(file system save the crc file)
.to(Push file to the sftp server);
from("direct:save-file")
.to(save the file)
.to(Push file to the sftp server);

0 kb file created once FTP is done in java

I am trying to FTP a file on to a remote machine. Below is my code :-
FTPClient ftpClient = new FTPClient();
ftpClient.connect("home.abc.com");
ftpClient.login("remote", "guesst12");
int replyCode = ftpClient.getReplyCode();
ftpClient.changeWorkingDirectory("share"))
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream input = new FileInputStream(new File("H:/testFile.txt"));
OutputStream out = ftpClient.storeFileStream("testFile.txt");
Util.copyStream(input, out);
out.close();
input.close();
ftpClient.completePendingCommand()
ftpClient.logout();
ftpClient.disconnect();
When i execute this piece of code, the code is executed without any issues, but at the remote machine, when i check the file, the file is being created, but with no content (OKB) file. Am i missing something in code.
[Update] :
I tried with the following code for storing file :-
if(ftpClient.storeFile("testCopy.txt", input)) {
System.out.println("File Stored Successfully");
}
System.out.println(ftpClient.getReplyString());
Now the reply code i recieved is :- 451 Failure writing to local file. What does that means.
Thanks
After looking at it over and over I keep coming up with different things.
Are you sure that the InputStream is reading the file before your copying the stream? Because I'm not sure FileInputStream read's the file on initiation.
I suspect that the problem is inUtil.copyStream, which code you didn't provide. I highly recommend that you use IOutils from Apache Commons IO to copy streams.
Looking at older questions here with similar problems, it looks like you hit a bug of the Commons-NET library (of which the FTPClient is a part).
Try to install a newer version (3.0.1 or later), or an earlier version (2.2) to fix this.
One of the reasons running into FTP error 451 when trying to copy a file over FTP
especially if you see 0 sized file created on the server side,
is probably due to No Space on Disk.

xlsx file corrupted during downloading from server

Folks, here is the situation:
confirmed that the xlsx file is good on the disc.
I copied the file from server and can open without problems.
using FileInputStream and BufferedInputStream to handle the client side download function.
The download function i mean user can download the file by clicking a hyperlink, and a servelet call was made to the java class which uses FileInputStream and BufferedInputStream
Mime type was set correctly as application/vnd.openxmlformats-officedocument.spreadsheetml.shee
After download the file successfully, it will give a "converted failed" error while trying to use office 2003 to open this xlsx file.
any thoughts?
Thanks!
There are bytes which didn't belong in the HTTP response body or were simply missing there.
It's impossible to point out the actual root cause based on the information given as far. You have to check if the right bytes were written from local disk file system to the HTTP response body.

Should retrieveFile of commons FTPClient remove file from remote server

We are using Commons FTPClient to retrieve files from an ftp server. Our code is similar to:
FTPClient ftpClient= new FTPClient();
ftpClient.connect(server);
ftpClient.login(username, password);
FileOutputStream out = new FileOutputStream(localFile);
ftpClient.retrieveFile(remoteFile, out)
When we run this code the file is moved from the FTP Server instead of copied. Just wondering is this expected behavior?
If this is expected behavior what is the best approach to retrieve a copy of the file from the server but leave a copy of the file on the server? (We do not have access to write to the FTP Server so we cannot write the file back to the server)
Any help appreciated,
Thanks
It is very strange behavior. I have just examined the code of FTPClient and did not see something that may remove the remote file. I believe that this is a configuration of your FTP server.
To check it I'd recommend you to try other FTP client. For example unix command line utility ftp or fget or regular web browser.
I wish you good luck.

Categories

Resources