Solved

FTP a zip file with java

Posted on 2009-05-15
5
387 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert ArrayList of custom objects to JSON 4 53
topping1 challenge 7 53
for i loop in grovy 1 34
maven example error 3 36
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now