Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file from one server to another

Tags:

java

I need to copy a text file from one server to another (both the servers are Linux). How do I do that in Java?

like image 255
Raj Avatar asked Sep 28 '10 12:09

Raj


2 Answers

If you need to copy files from accessible file systems go with Andreas' answer.

If you want a general approach that abstracts from the protocol underneath, have a look at Apache Commons VFS. It provides a common api for resources available through a number of protocols:

  • FTP
  • Local Files
  • HTTP and HTTPS
  • SFTP
  • Temporary Files
  • Zip, Jar and Tar (uncompressed, tgz or tbz2)
  • gzip and bzip2
  • res
  • ram
  • mime
like image 140
Sean Patrick Floyd Avatar answered Nov 03 '22 07:11

Sean Patrick Floyd


Easist if you're able to use apache commons-io: the FileUtils class has convenient methods to copy files:

FileUtils.copyFileToDirectory(srcFile, targetDirectory);

(as you talked about text files I assume, your application has access to both file systems)

like image 30
Andreas Dolk Avatar answered Nov 03 '22 07:11

Andreas Dolk