1

我正在阅读 SWIFT 消息 (MT103) 并将它们加载到数据库中。之后我需要删除该文件以避免在接下来的循环中进行第二次解析。但是它接缝prowide不会InputStream在解析消息后自动关闭。如何关闭 InputStream 以便成功归档文件?当我尝试使用存档文件时FileUtils出现错误:Failed to delete original file '\home\swift\991904100973103097.out' after copy to '\home\Outswift\991904100973103097.out'

public static void loadMessageInformationToDatabase(String sourceDir, File archiveDir) {
    log.debug("Listening to Incoming SWIFT Messages");
    File[] files = new File(sourceDir).listFiles();
    log.debug("Found " + files.length + " SWIFT Files for Processing");
    if (files.length > 0) {
        for (int i = 0; i < files.length; i++) {
            log.debug("Attepting to process file " + files[i].getAbsolutePath());
            int swiftSerialNumner = 0;
            try {
                MtSwiftMessage msg = MtSwiftMessage.parse(files[i]);
                String mtNumber = msg.getMessageType();
                String receiver = msg.getReceiver();
                String sender = msg.getSender();
                byte[] message = IOUtils.toByteArray(new FileInputStream(files[i]));
                String pde = msg.getPde();
                String instructedCurrency = "";
                double instructedAmount = 0.00;
                String inOutIndicator = "";
                if (msg.isIncoming()) {
                    inOutIndicator = "I";
                } else if (msg.isOutgoing()) {
                    inOutIndicator = "O";
                }
                String senderReference = "";
                if (mtNumber.equalsIgnoreCase("103")) {
                    MT103 model = MT103.parse(files[i]);
                    List<Field> fields = model.getFields();
                    for (Field field : fields) {
                        if (field.getName().equalsIgnoreCase("20")) {
                            senderReference = field.getValue();
                        }
                        if (field.getName().equalsIgnoreCase("33B")) {
                            instructedCurrency = field.getValue().substring(0, 3);
                            instructedAmount = Double.parseDouble(field.getValue().substring(3).replace(",", "."));
                        }
                    }
                }
                swiftSerialNumner = Swift.addSwiftMessage(mtNumber, receiver, message, "N", null, null, null, null, "S", "SYSTEM", new Date(), "Y", new Date(), "SYSTEM", new Date(), "SYSTEM", null, inOutIndicator, null, null, null, pde, "Y", receiver, sender, null, senderReference, null, instructedAmount, instructedCurrency, null, null, null, null, null, "N", "Y", "N", Gct.getBankId(), "Y");
                if (swiftSerialNumner > 0) {
                    log.debug("File " + files[i].getAbsolutePath() + " Parsed Successfully");
                    if (Swift.isDuplicateReference(senderReference)) {
                        Swift.markSwiftMessageAsDuplicate(swiftSerialNumner);
                        log.warn("SWIFT Serial " + swiftSerialNumner + " Marked as Duplicate");
                    }
                } else {
                    log.error("Error in Parsing File Name " + files[i].getAbsolutePath());
                }

                FileUtils.moveFileToDirectory(files[i], archiveDir, true);//Error comes on this line
                log.debug("SWIFT File " + files[i] + " archived in " + archiveDir.getAbsolutePath());
            } catch (IOException | NumberFormatException asd) {
                log.error("SWIFT Processing Error: " + asd.getMessage());
            }
        }
    }
}
4

1 回答 1

1

尝试这个:

FileInputStream fis = new FileInputStream(files[i])
byte[] message = IOUtils.toByteArray(fis);

并添加 finally 子句并关闭 fis:

} catch (IOException | NumberFormatException asd) {
    log.error("SWIFT Processing Error: " + asd.getMessage());
} finally {
    fis.close()
}
于 2019-04-10T08:13:30.953 回答