0 kb file created once FTP is done in java - 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.

Related

FTP File Upload - Filename Encoding Error

I'm using apache's commons-net 3.5 to upload files to a remote FTP server, and setting the connection encoding to UTF-8 like below, before openning the connection.
ftpClient.setAutodetectUTF8(true);
ftpClient.setControlEncoding("UTF-8");
And this is the part that sends the file
private void uploadFile(byte[] data, String path, String fileName, FTPClient ftpClient) throws IOException {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
boolean uploadSuccess = ftpClient.storeUniqueFile(fileName, new ByteArrayInputStream(data));
Here is the problem; when sending files whose name contains Turkish characters, the result file is named incorrectly. Like,
Kimlikş.pdf -> KimlikÅ.pdf
But if I use ftp4j, everything works fine. Did anyone have this before? The data ships incrorrectly from my side. I monitored the traffic with Microsoft Network Monitor. Here is the raw request:
FTP FTP:Request from Port 62642,'STOR KimlikÅ.pdf' {TCP:5879, IPv4:134}
try to use WinSCP - it was working like a charm.
https://winscp.net/eng/download.php
no FileZilla, no TotalCommander - only WinSCP.
and by copy again (for example I had about 6k files and only 50 with cyrilic) only give at question prompt "no to all" and WinSCP copies only left files. great !
5 stars to the free software !
I guess there was a bug with apache's commons-net, so I migrated to ftp4j and the problem was no more.
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>ftp4j</artifactId>
<version>1.6</version>
</dependency>

How to copy a file in FTP server using FTPClient in 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);

Java: Upload file from URL directly to FTP server with a library

It is possible to transfer a file from a url (http://... .exe/ .zip ...) directly to a ftp server in Java 7 (with some additional libraries eg from Apache?
There is support for both HTTP and at least limited FTP support through the standard API class java.net.URL. You have only restricted access to specific FTP features like setting the transfer mode, but in most cases it works.
If you add Apache Commons IO, you can use the IOUtils class to copy directly from the HTTP server to the FTP server:
InputStream in = new URL("http://host/path").openStream();
OutputStream out =
new URL("ftp://user:pass#host/path").openConnection().getOutputStream();
IOUtils.copy(in, out);
in.close();
out.close();
If you don't want to add a dependency on Commons IO, you just have to write a few lines to copy the data without 3rd party library support:
byte[] buffer = new byte[16384];
int r = 0;
while ((r=in.read(buffer))>=0) {
out.write(buffer, 0, r);
}
I don't believe it's possible in general to instruct a web server to upload a file directly to an FTP server, but it's definitely possible to write a program to download the file yourself and then upload it to an FTP server.

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