我在自由配置文件上部署了小型应用程序(.ear)。 Bean之一是在Windows共享目录中查找csv文件,处理后将其移动。我正在使用SMBJ来完成这项工作(复制到dest,然后将其删除)。应用程序每分钟醒来以执行此任务。读取文件后,它将文件复制到dest,但是在删除时,它将标记为删除,而不是实际删除。
作业的第二次迭代再次选择了该文件,但由于将其标记为删除而无法读取。有人告诉我关闭文件句柄,而我的代码正在处理所有这些问题,但我仍然面临这些问题。谁能告诉我我在做什么错了。
public boolean moveFile(String srcFilePath, String destFilePath) {
try {
DiskShare srcShare=null, destShare=null;
Connection srcConnection=null, destConnection=null;
Session srcSession=null, destSession=null;
String srcPath=null, destPath=null;
SMBClient client = new SMBClient();
String srcParts[] = getPartsOfSharedPath(srcFilePath);
srcConnection = client.connect(srcParts[0]);
srcSession = srcConnection.authenticate(auth);
srcShare = (DiskShare)srcSession.connectShare(srcParts[1]);
srcPath = srcParts[2];
String destParts[] = getPartsOfSharedPath(destFilePath);
destConnection = client.connect(destParts[0]);
destSession = destConnection.authenticate(auth);
destShare = (DiskShare)destSession.connectShare(destParts[1]);
destPath = destParts[2];
//System.out.println("Remote to Remote");
Set<SMB2ShareAccess> s = new HashSet<>();
s.add(SMB2ShareAccess.ALL.iterator().next());
try(com.hierynomus.smbj.share.File srcFile = srcShare.openFile(srcPath, EnumSet.of(AccessMask.GENERIC_READ), null, s, SMB2CreateDisposition.FILE_OPEN, null);
InputStream in = srcFile.getInputStream();
com.hierynomus.smbj.share.File destFile = destShare.openFile(destPath, EnumSet.of(AccessMask.GENERIC_WRITE), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE_IF, null);
OutputStream out = destFile.getOutputStream()){
int bytesRead;
byte[] buffer = new byte[1024];
while((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
}catch(IOException ex){
logger.logError("", ex);
}
}
client.close();
return deleteFile(srcFilePath);
} catch(SMBApiException | IOException e){
logger.logError("",e);
}
return false;
}
public boolean deleteFile(String fileName){
try{
String srcParts[] = getPartsOfSharedPath(fileName);
try (SMBClient client = new SMBClient();
Connection srcConnection = client.connect(srcParts[0]);
Session srcSession = srcConnection.authenticate(auth);
DiskShare srcShare = (DiskShare)srcSession.connectShare(srcParts[1])) {
srcShare.rm(srcParts[2]);
return true;
}
}
catch(IOException ex){
logger.logError("",ex);
}
返回假; }