我會第一時間發佈代碼,然後闡述:無法刪除文件,因爲它是在使用
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
仍以某種方式仍在使用中。任何想法如何解鎖文件,以便我可以刪除它?備註:如果驗證成功,則刪除文件夾也會成功。
我找不到'的getInputStream()'方法。 –
我需要使用'StreamSource'而不是'Source'來獲取該方法。 –
^^ true,我在示例代碼中更正了它。現在在工作嗎? – mondjunge