Skip to content

Instantly share code, notes, and snippets.

@heatzync
Created May 4, 2012 13:54
Show Gist options
  • Save heatzync/2594934 to your computer and use it in GitHub Desktop.
Save heatzync/2594934 to your computer and use it in GitHub Desktop.
The "file" cannot be moved (deleted) on Windows, but it can be moved on linux. Why?
Logger logger = LoggerFactory.getLogger();
FTPClient ftpClient = new FTPClient(); // from commons-net
public void upload(File file) {
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
this.logger.debug("Uploading file");
if (this.ftpClient.storeFile(file.getName(), is)) {
this.moveToSentDirectory(file);
}
else {
this.logger.error("Failure uploading file");
}
}
catch (Exception e) {
this.logger.error("Failure uploading file", e);
}
finally {
Closeables.close(is); // closes silently, but logs errors
}
}
private boolean moveToSentDirectory(File from) {
File to = new File(this.workDirectory + File.separatorChar + this.sentDirectory + File.separatorChar + from.getName());
try {
this.logger.debug("Moving file [from={},to={}]", from, to);
Files.createParentDirs(to);
Files.move(from, to); // this throws an IOException
return true;
}
catch (IOException ioe) {
this.logger.error("Failure moving file [from=" + from + ",to=" + to + "]", ioe);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment