RBY
RBY

Reputation: 11

file treansfer from windows OS to unix in Java

I create a pdf file in runtime (in Windows OS). I need to copy it to another location, it might be on UNIX or windows. Is there a java class I can do it with? and how? Thanks.

Upvotes: 1

Views: 4254

Answers (2)

Stephen C
Stephen C

Reputation: 718738

If you place the file in the directory space of an FTP server (on your Windows machine), you can use URLConnection in a Java app on a remove client to fetch it. See @Mohamed Saligh's answer for example code. (The key is to use an "ftp:" URL, and to force the transfer type to be binary.)

Other resources that may help include the Apache Commons FTP Client library, and the Apache Mina FTP server. The FTP client library would allow you to "push" the file to an FTP server on Windows / UNIX ... as well as "pull" it as URLConnection does.

There are various other Java FTP clients, servers and libraries "floating around the interwebs" ... according to a certain well-known search engine :-).

Upvotes: 0

Mohamed Saligh
Mohamed Saligh

Reputation: 12339

URL url = 
    new URL("ftp://username:[email protected]/file.pdf;type=i");
URLConnection con = url.openConnection();
BufferedInputStream in = 
    new BufferedInputStream(con.getInputStream());
FileOutputStream out = 
    new FileOutputStream("C:\\file.pdf");

int i = 0;
byte[] bytesIn = new byte[1024];
while ((i = in.read(bytesIn)) >= 0) {
    out.write(bytesIn, 0, i);
}
out.close();
in.close();

Upvotes: 5

Related Questions