我先贴出代码,再详细说明:
public void createPackage(String uploadedZipLocation) throws BadException, IOException, SAXException {
String extractedFolderPath = uploadedZipLocation.split("\\.zip")[0];
File tempFolder = null;
Source xml = null;
try {
xml = new StreamSource(extractedFolderPath + "/web.xml");
validatePkgXml(xml, extractedFolderPath + "/web.xml");
xml = null;
} catch (IOException e) {
throw e;
} catch (BadException bpe) {
xml = null;
tempFolder = new File(extractedFolderPath);
FileUtils.deleteDirectory(tempFolder); // **** Can't delete the folder because it is in use. web.xml is still being read in validatePkgXml I think
throw bpe;
}
}
private void validatePkgXml(Source xmlStream, String xmlPath) throws BadException, IOException, SAXException{
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("/C:/workspacesFresh2/web.xsd"));
Validator validator = schema.newValidator();
validator.setErrorHandler(new PackageXMLValidationErrorHandler());
try {
validator.validate(xmlStream);
logger.info(xmlStream.getSystemId() + " is valid");
} catch (SAXException e) {
logger.error(xmlStream.getSystemId() + " is NOT valid");
throw new BadException(xmlPath, e.getLocalizedMessage());
}
}
我正在尝试获取一个 xml 文件并根据xsd
模式对其进行验证。如果验证失败,我想删除包含 xml 文件的文件夹。验证失败时,我无法删除该文件夹,因为该文件夹web.xml
仍在使用中。我尝试在验证失败后将其设置为,但Source
不知何故仍在使用中。任何想法如何解锁文件以便我可以删除它?旁注:如果验证成功,则删除文件夹也成功。null
web.xml