Solved

FTP Zip Files

Posted on 2007-11-28
3
181 Views
Last Modified: 2010-03-30
This is a second part to the my other question.

I am able to create a file on my server via FTP.  But the file has no data.

I am very new Java so any help would be great.
//****************************************************Start FTP

        File file1 = new File("c:\\target.zip");

        

        try {

        URL url = new URL("ftp://user:user@server.server.server.com/target.zip;type=i");

        URLConnection con = url.openConnection();

        FileInputStream in = new FileInputStream(file1);

        FileOutputStream out = new FileOutputStream("C:\\target.zip");

        //OutputStream out = con.getOutputStream();

        int i = 0;

        byte[] bytesIn = new byte[1024];

        while ((i = in.read(bytesIn)) > 0) {

	        out.write(bytesIn, 0, i);

        }

        out.close();

        in.close();

        } 

        catch (IOException e) {

        }    

//*************************************************End FTP

Open in new window

0
Comment
Question by:Jaziar
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20368466
0
 
LVL 9

Assisted Solution

by:ysnky
ysnky earned 50 total points
ID: 20368812
but your writing to your local file.

you have to comment this line;
FileOutputStream out = new FileOutputStream("C:\\target.zip");

and uncomment this line;
OutputStream out = con.getOutputStream();

your new code must look like;

//FileOutputStream out = new FileOutputStream("C:\\target.zip");
OutputStream out = con.getOutputStream();
0
 
LVL 92

Accepted Solution

by:
objects earned 75 total points
ID: 20369171
> as well as the changes ysnky posted you also need to change you while loop

        File file1 = new File("c:\\target.zip");
       
        try {
        URL url = new URL("ftp://user:user@server.server.server.com/target.zip;type=i");
        URLConnection con = url.openConnection();
        FileInputStream in = new FileInputStream(file1);
        OutputStream out = con.getOutputStream();
        int i = 0;
        byte[] bytesIn = new byte[1024];
        while ((i = in.read(bytesIn)) >= 0) {
                out.write(bytesIn, 0, i);
        }
        out.close();
        in.close();
        }
        catch (IOException e) {
        }    
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

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