Solved

FTP a zip file with java

Posted on 2009-05-15
5
392 Views
Last Modified: 2012-05-07
I'm using this piece of code in order to upload files to an ftp server,
and it works ok with plain file (eg. a TXT file)
but when I'm trying to upload a ZIP file ,
the file that uploaded to the server is corrapted.
Any suggestons ? ?

Thanks
public class testFTP {
 
    public static final void main(String[] args) {
        String server      = null;     server       = args[0]
        String username = null;     username = args[1];
        String password = null;    password = args[2];
        String directory   = null;     directory   = args[3];
        
        try{
        FTPClient ftp=new FTPClient();
        
        ftp.connect(server);
        ftp.login(username, password);
        
        ftp.changeWorkingDirectory(directory);
        
        String dir = System.getProperty("user.dir")+"\\";
        String filename = dir+"my_file.zip";
        
        File f = new File(filename);
        FileInputStream fis = new FileInputStream(filename);
        ftp.storeFile(f.getName(), fis);
        ftp.logout();
        fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}

Open in new window

0
Comment
Question by:nikos270
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24394057
Call binary() on the client before uploading
0
 

Author Comment

by:nikos270
ID: 24394073
you mean something like this:
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);

0
 
LVL 12

Accepted Solution

by:
Gibu George earned 250 total points
ID: 24394094
If you are using apache commones net FTPClient
set the file type as FTP.BINARY_FILE_TYPE
ftp.setFileType(FTP.BINARY_FILE_TYPE);
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24394121
The transfer modes are

 BLOCK_TRANSFER_MODE, COMPRESSED_TRANSFER_MODE,  STREAM_TRANSFER_MODE
by default it uses STREAM_TRANSFER_MODE
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24394311
>>you mean something like this:

yes
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 90
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
Opening PDF on button click and fill new document 2 38
by zero exception 10 41
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question