简体   繁体   中英

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.

URL url = 
    new URL("ftp://username:password@ftp.localhost/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();

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 :-).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM