Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

FTP Zip Files

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
Jaziar
Asked:
Jaziar
2 Solutions
 
ysnkyCommented:
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
 
objectsCommented:
> 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now