大きなファイル、またはファイルとファイルに関連付けられているすべてのシステム権限をコピーする必要がある場合は、java internal File.copy()を使用するとコストがかかりすぎるため、すべての負荷をシステムにオフロードできます。
次のトリックを試してください。まず、exec()の引数としてユーザー文字列配列を使用します。次に、/C引数を指定した「cmd」コマンドの後にパイプで「xcopy」を実行します。isWindows()呼び出しを行う行の近くのサンプルコードを見てください。
秘訣は、xcopyコマンドがCMDシェル内で実行され、/Cが正常に実行された後にコマンドを終了することです。CMD.exeの詳細。
public int sysCopyFile(Resource fromResource, Resource toResource) throws ServiceException {
int returnCode = -1;
try {
String[] copyCommand = null;
if ( IOUtils.isWindows() ) {
copyCommand = new String[] {"cmd", "/C", "copy", "/Y", fromResource.getFile().getAbsolutePath(), toResource.getFile().getAbsolutePath()};
} else if ( IOUtils.isUnix() || IOUtils.isMac() ) {
copyCommand = new String[] {"/bin/cp", "-pr", fromResource.getFile().getAbsolutePath(),toResource.getFile().getAbsolutePath()};
}
final Process p = Runtime.getRuntime().exec(copyCommand);
new StreamLogger(p.getErrorStream(), log, StreamLogger.WARN);
new StreamLogger(p.getInputStream(), log, StreamLogger.DEBUG);
returnCode = p.waitFor();
if (returnCode != 0) throw new ServiceException("Unable to to copy. Command: {" + copyCommand[0] + "} has returned non-zero returnCode: " + returnCode);
} catch (IOException e) {
throw new ServiceException(e);
} catch (InterruptedException e) {
throw new ServiceException(e);
}
return returnCode;
}