2013-08-06 89 views
0

Please mention me the link if this is a duplicate and has the apt answer.複製本地共享文件,將Linux本地驅動器

我的項目的實際主題是從本地服務器基於OS的任何機器複製「.ZIP」文件(安裝文件)。讓路徑爲//123.1.23.3

windows我可以直接複製它,如FileUtils.copyFiles(srcFile, destFile)

Linux,我不知道如何實現它。我甚至不喜歡考慮srcFileSmbFile(i.e samba file)destFile是一個File和這裏的問題是,無論是我應該使用

 `FileUtils.copyFiles(srcFile, destFile)`. (If both of them are 'File's) 
or 
     `srcFile.copyTo(destFile)` (If both files are 'SmbFile's) 

但這裏既不可能bcoz srcFile - SmbFile(file in local server)destFile - File(local drive)

如果有人建議我用streams複製它,是有在Linux任何方式zip文件直接拷貝無需解壓它,因爲我在窗口做了(在一個單一的步驟)。

,因爲我有一個單獨的methodsextracttar文件中windowslinux分別,如果我在這裏使用流我需要提取它,也就不需要上述獨立方法。

我想我說得很清楚,謝謝。

+2

如果你安裝正確(smbmount),然後FileUtils.copyFiles應該工作來實現。你有沒有得到任何錯誤? – Jayan

+0

嗨笏你是否意味着坐騎,或者要做什麼? – abu

+0

你到目前爲止嘗試過什麼?在Linux機器上,你可以在命令行上執行copy操作嗎? Google smbmount,你會得到更多的信息:從http://linux.die.net/man/8/smbmount開始 – Jayan

回答

0

它可以使用IOUtils.copy(src.getInputStream(), new FileOutputStream(destFile));

0

嗨,你可以使用Java NIO包的文件複製實用程序

import java.nio.file.*; 
import static java.nio.file.StandardCopyOption.*; 
import java.nio.file.attribute.*; 
import static java.nio.file.FileVisitResult.*; 
import java.io.IOException; 
import java.util.*; 

public class Copy { 

    /** 
    * Returns {@code true} if okay to overwrite a file ("cp -i") 
    */ 
    static boolean okayToOverwrite(Path file) { 
     String answer = System.console().readLine("overwrite %s (yes/no)? ", file); 
     return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")); 
    } 

    /** 
    * Copy source file to target location. If {@code prompt} is true then 
    * prompt user to overwrite target if it exists. The {@code preserve} 
    * parameter determines if file attributes should be copied/preserved. 
    */ 
    static void copyFile(Path source, Path target, boolean prompt, boolean preserve) { 
     CopyOption[] options = (preserve) ? 
      new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } : 
      new CopyOption[] { REPLACE_EXISTING }; 
     if (!prompt || Files.notExists(target) || okayToOverwrite(target)) { 
      try { 
       Files.copy(source, target, options); 
      } catch (IOException x) { 
       System.err.format("Unable to copy: %s: %s%n", source, x); 
      } 
     } 
    } 

    /** 
    * A {@code FileVisitor} that copies a file-tree ("cp -r") 
    */ 
    static class TreeCopier implements FileVisitor<Path> { 
     private final Path source; 
     private final Path target; 
     private final boolean prompt; 
     private final boolean preserve; 

     TreeCopier(Path source, Path target, boolean prompt, boolean preserve) { 
      this.source = source; 
      this.target = target; 
      this.prompt = prompt; 
      this.preserve = preserve; 
     } 

     @Override 
     public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { 
      // before visiting entries in a directory we copy the directory 
      // (okay if directory already exists). 
      CopyOption[] options = (preserve) ? 
       new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0]; 

      Path newdir = target.resolve(source.relativize(dir)); 
      try { 
       Files.copy(dir, newdir, options); 
      } catch (FileAlreadyExistsException x) { 
       // ignore 
      } catch (IOException x) { 
       System.err.format("Unable to create: %s: %s%n", newdir, x); 
       return SKIP_SUBTREE; 
      } 
      return CONTINUE; 
     } 

     @Override 
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 
      copyFile(file, target.resolve(source.relativize(file)), 
        prompt, preserve); 
      return CONTINUE; 
     } 

     @Override 
     public FileVisitResult postVisitDirectory(Path dir, IOException exc) { 
      // fix up modification time of directory when done 
      if (exc == null && preserve) { 
       Path newdir = target.resolve(source.relativize(dir)); 
       try { 
        FileTime time = Files.getLastModifiedTime(dir); 
        Files.setLastModifiedTime(newdir, time); 
       } catch (IOException x) { 
        System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x); 
       } 
      } 
      return CONTINUE; 
     } 

     @Override 
     public FileVisitResult visitFileFailed(Path file, IOException exc) { 
      if (exc instanceof FileSystemLoopException) { 
       System.err.println("cycle detected: " + file); 
      } else { 
       System.err.format("Unable to copy: %s: %s%n", file, exc); 
      } 
      return CONTINUE; 
     } 
    } 

    static void usage() { 
     System.err.println("java Copy [-ip] source... target"); 
     System.err.println("java Copy -r [-ip] source-dir... target"); 
     System.exit(-1); 
    } 

    public static void main(String[] args) throws IOException { 
     boolean recursive = false; 
     boolean prompt = false; 
     boolean preserve = false; 

     // process options 
     int argi = 0; 
     while (argi < args.length) { 
      String arg = args[argi]; 
      if (!arg.startsWith("-")) 
       break; 
      if (arg.length() < 2) 
       usage(); 
      for (int i=1; i<arg.length(); i++) { 
       char c = arg.charAt(i); 
       switch (c) { 
        case 'r' : recursive = true; break; 
        case 'i' : prompt = true; break; 
        case 'p' : preserve = true; break; 
        default : usage(); 
       } 
      } 
      argi++; 
     } 

     // remaining arguments are the source files(s) and the target location 
     int remaining = args.length - argi; 
     if (remaining < 2) 
      usage(); 
     Path[] source = new Path[remaining-1]; 
     int i=0; 
     while (remaining > 1) { 
      source[i++] = Paths.get(args[argi++]); 
      remaining--; 
     } 
     Path target = Paths.get(args[argi]); 

     // check if target is a directory 
     boolean isDir = Files.isDirectory(target); 

     // copy each source file/directory to target 
     for (i=0; i<source.length; i++) { 
      Path dest = (isDir) ? target.resolve(source[i].getFileName()) : target; 

      if (recursive) { 
       // follow links when copying files 
       EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); 
       TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve); 
       Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc); 
      } else { 
       // not recursive so source must not be a directory 
       if (Files.isDirectory(source[i])) { 
        System.err.format("%s: is a directory%n", source[i]); 
        continue; 
       } 
       copyFile(source[i], dest, prompt, preserve); 
      } 
     } 
    } 
} 
相關問題