public static void synchronize(File source, File destination, boolean smart, long chunkSize) throws IOException { if ( chunkSize <= 0 ) { System.out.println("Chunk size must be positive: using default value." ); chunkSize = DEFAULT_COPY_BUFFER_SIZE; } if ( source.isDirectory() ) { if ( !destination.exists() ) { if ( !destination.mkdirs() ) { throw new IOException( "Could not create path " + destination ); } } else if ( !destination.isDirectory() ) { throw new IOException( "Source and Destination not of the same type:" + source.getCanonicalPath() + " , " + destination.getCanonicalPath() ); } String[] sources = source.list(); Set<String> srcNames = new HashSet<String>( Arrays.asList( sources ) ); String[] dests = destination.list();
//delete files not present in source for ( String fileName : dests ) { if ( !srcNames.contains( fileName ) ) { delete( new File( destination, fileName ) ); } } //copy each file from source for ( String fileName : sources ) { File srcFile = new File( source, fileName ); File destFile = new File( destination, fileName ); synchronize( srcFile, destFile, smart, chunkSize ); } } else { if ( destination.exists() && destination.isDirectory() ) { delete( destination ); } if ( destination.exists() ) { long sts = source.lastModified() / FAT_PRECISION; long dts = destination.lastModified() / FAT_PRECISION; //do not copy if smart and same timestamp and same length if ( !smart || sts == 0 || sts != dts || source.length() != destination.length() ) { copyFile( source, destination, chunkSize ); } } else { copyFile( source, destination, chunkSize ); } } }
private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException { FileInputStream is = null; FileOutputStream os = null; try { is = new FileInputStream( srcFile ); FileChannel iChannel = is.getChannel(); os = new FileOutputStream( destFile, false ); FileChannel oChannel = os.getChannel(); long doneBytes = 0L; long todoBytes = srcFile.length(); while ( todoBytes != 0L ) { long iterationBytes = Math.min( todoBytes, chunkSize ); long transferredLength = oChannel.transferFrom( iChannel, doneBytes, iterationBytes ); if ( iterationBytes != transferredLength ) { throw new IOException( "Error during file transfer: expected " + iterationBytes + " bytes, only " + transferredLength + " bytes copied." ); } doneBytes += transferredLength; todoBytes -= transferredLength; } } finally { if ( is != null ) { is.close(); } if ( os != null ) { os.close(); } } boolean successTimestampOp = destFile.setLastModified( srcFile.lastModified() ); if ( !successTimestampOp ) { System.out.println("Could not change timestamp for {}. Index synchronization may be slow. " + destFile ); } }
public static void delete(File file) { if ( file.isDirectory() ) { for ( File subFile : file.listFiles() ) { delete( subFile ); } } if ( file.exists() ) { if ( !file.delete() ) { System.out.println( "Could not delete {}" + file ); } } } }
Burada da bilgisayardan uzak sunucuya dosyalar aktarılıyor.Link
/** * Upload a whole directory (including its nested sub directories and files) * to a FTP server. * * @param ftpClient * an instance of org.apache.commons.net.ftp.FTPClient class. * @param remoteDirPath * Path of the destination directory on the server. * @param localParentDir * Path of the local directory being uploaded. * @param remoteParentDir * Path of the parent directory of the current directory on the * server (used by recursive calls). * @throws IOException * if any network or IO error occurred. */ public static void uploadDirectory(FTPClient ftpClient, String remoteDirPath, String localParentDir, String remoteParentDir) throws IOException {
if (item.isFile()) { // upload the file String localFilePath = item.getAbsolutePath(); System.out.println("About to upload the file: " + localFilePath); boolean uploaded = uploadSingleFile(ftpClient, localFilePath, remoteFilePath); if (uploaded) { System.out.println("UPLOADED a file to: " + remoteFilePath); } else { System.out.println("COULD NOT upload the file: " + localFilePath); } } else { // create directory on the server boolean created = ftpClient.makeDirectory(remoteFilePath); if (created) { System.out.println("CREATED the directory: " + remoteFilePath); } else { System.out.println("COULD NOT create the directory: " + remoteFilePath); }
// upload the sub directory String parent = remoteParentDir + "/" + item.getName(); if (remoteParentDir.equals("")) { parent = item.getName(); }
localParentDir = item.getAbsolutePath(); uploadDirectory(ftpClient, remoteDirPath, localParentDir, parent); } } } } /** * Upload a single file to the FTP server. * * @param ftpClient * an instance of org.apache.commons.net.ftp.FTPClient class. * @param localFilePath * Path of the file on local computer * @param remoteFilePath * Path of the file on remote the server * @return true if the file was uploaded successfully, false otherwise * @throws IOException * if any network or IO error occurred. */ public static boolean uploadSingleFile(FTPClient ftpClient, String localFilePath, String remoteFilePath) throws IOException { File localFile = new File(localFilePath);
Yukarıdaki kodlardan yararlanmaya çalıştım ama yapamadığım yer ikinci kısımdaki kodları tam olarak senkronizasyon yapacak şekilde düzenlemek, dropbox benzeri bir uygulama yapmak web tarafı hazır gibi. İlk kısımdaki kodlar tam olarak senkronize işlemini yapıyor ama localde, ilk kısımdaki hedef klasörü uzak sunucu(ftp) olacak şekilde nasıl değiştirebilirim. İkinci kısımda dosyaları sunucuya upload ediyor ama herhangi bir kontrol yok, dosya sunucuda var mı diye. Ayrıca kaynak klasörde olmayan dosyaları da silmiyor. Epey uğraştım ama java bilgim yetersiz. Yardımcı olursanız sevinirim.