调用方法Files.write(Path.of(fileForDocumentContent.getAbsolutePath()),htmlDocument.getContent()有问题;即使文件确实存在,它也会抛出java.nio.file.NoSuchFileException.。
我想指出,在我的本地机器上,它工作得很好,但是它不工作在DEV机器上,它位于linux上。
我的代码是:
@Override
public Document convert(Document document, DocumentFormat documentFormat) {
Document htmlDocument = htmlDocumentConverter.convert(document, documentFormat);
File fileForDocumentContent = null;
File fileForConvertedContent = null;
try {
log.info("Converting document from {} to {}", getSourceFormat().toString(), getTargetFormat().toString());
CalibreConfigData calibreData = calibreConfig.getConfigurationData(CalibreConversion.HTML_TO_DOCX);
fileForDocumentContent = new File(calibreData.getSourceFilePath());
fileForConvertedContent = new File(calibreData.getConvertedFilePath());
// \/ doesn't work \/
Files.write(fileForDocumentContent.toPath(), htmlDocument.getContent());
Runtime.getRuntime().exec(calibreData.getCalibreCommand()).waitFor();
byte[] convertedFileAsBytes = Files.readAllBytes(fileForConvertedContent.toPath());
return new Document(convertedFileAsBytes);
} catch (InterruptedException | IOException e) {
log.error("Conversion failed due to problem: " + e);
throw new MetanormaConversionException("Conversion failed due to problem: " + e);
} finally {
deleteFilesIfPresent(fileForDocumentContent, fileForConvertedContent);
}
}fileForDocumentContent =新文件(calibreData.getSourceFilePath());
在path:ile:/usr/src/pok-document-service.jar!/BOOT-INF/classes!/5b1138f8-1805-4db8-a5ed-acab8509adc4.html下创建文件
而准确的错误看起来:
由于问题而导致
“转换失败: java.nio.file.NoSuchFileException: java.nio.file.NoSuchFileException
正如您所看到的,文件的两个路径看起来都是一样的,但是它会抛出一个异常。我试图将.getAbsolutePath()从.toPath()改为,但仍然出现了相同的问题。
发布于 2022-10-25 11:08:51
请注意,NoSuchFileException也可能意味着运行应用程序的进程没有足够的权限来创建/写入该文件,查看整个错误跟踪
如果您在本地环境中使用eclipse/vscode或类似的代码,并且您的代码正在写入已编译的“目标”文件夹,这对我来说是有意义的。
查看错误的路径,看起来您正在使用spring引导执行应用程序,并且您正在尝试修改JAR文件中的一个文件,在这种情况下,您应该有一个要写入的外部文件夹。
希望它能帮上忙
发布于 2022-10-25 11:32:32
显然,您没有传递有效的文件系统路径。为了使Java能够更改您指定的文件,下面的bash命令也可以工作:
touch "ile:/usr/src/pok-document-service.jar!/BOOT-INF/classes!/5b1138f8-1805-4db8-a5ed-acab8509adc4.html"要使文件写入工作,您需要一个有效的路径,例如:
fileForDocumentContent = new File("/tmp/5b1138f8-1805-4db8-a5ed-acab8509adc4.html");https://stackoverflow.com/questions/74192999
复制相似问题