Şimdi Ara

Java Local Klasör - Uzak Sunucu Senkronize

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
1 Misafir - 1 Masaüstü
5 sn
1
Cevap
0
Favori
1.142
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • Burada bilgisayar üzerindeki iki dosyayı senkronize eden kod var.Link
    import java.io.File; 
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;


    /**
    * Utility class for synchronizing files/directories.
    *
    * @author Emmanuel Bernard
    * @author Sanne Grinovero
    * @author Hardy Ferentschik
    */
    public abstract class FileHelper {

    private static final int FAT_PRECISION = 2000;
    public static final long DEFAULT_COPY_BUFFER_SIZE = 16 * 1024 * 1024; // 16 MB


    public static boolean areInSync(File source, File destination) throws IOException {
    if ( source.isDirectory() ) {
    if ( !destination.exists() ) {
    return false;
    }
    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();

    // check for files in destination and not in source
    for ( String fileName : dests ) {
    if ( !srcNames.contains( fileName ) ) {
    return false;
    }
    }

    boolean inSync = true;
    for ( String fileName : sources ) {
    File srcFile = new File( source, fileName );
    File destFile = new File( destination, fileName );
    if ( !areInSync( srcFile, destFile ) ) {
    inSync = false;
    break;
    }
    }
    return inSync;
    }
    else {
    if ( destination.exists() && destination.isFile() ) {
    long sts = source.lastModified() / FAT_PRECISION;
    long dts = destination.lastModified() / FAT_PRECISION;
    return sts == dts;
    }
    else {
    return false;
    }
    }
    }

    public static void synchronize(File source, File destination, boolean smart) throws IOException {
    synchronize( source, destination, smart, DEFAULT_COPY_BUFFER_SIZE );
    }

    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 {

    System.out.println("LISTING directory: " + localParentDir);

    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
    for (File item : subFiles) {
    String remoteFilePath = remoteDirPath + "/" + remoteParentDir
    + "/" + item.getName();
    if (remoteParentDir.equals("")) {
    remoteFilePath = remoteDirPath + "/" + item.getName();
    }


    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);

    InputStream inputStream = new FileInputStream(localFile);
    try {
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    return ftpClient.storeFile(remoteFilePath, inputStream);
    } finally {
    inputStream.close();
    }
    }


    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.







  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.