开发者

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 ja开发者_如何学编程va 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 :-).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜