저는 Java로 Windows 또는 MacOS에서 사용하기 위해 배포되는 일부 소프트웨어를 개발해 왔습니다. 나는 애플 특유의 요구 때문에 순간에 아이맥으로 프로그래밍하고있다. 다음 방법은 도움말 파일 (.pdf)을 리소스에서 끌어와 로컬 컴퓨터에 표시 할 수 있도록 로컬로 복사합니다.Java - IOException : 파일을 열지 못했습니다 : 액세스가 거부되었습니다.
private void mHelpActionPerformed(java.awt.event.ActionEvent evt) {
String sourceFile = "resources/BRC2Help.pdf";
String destFile = "brc2help.pdf";
File f = new File (destFile);
if (!f.exists()) {
try {
URL url = ClassLoader.getSystemResource(sourceFile) ;
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
System.out.printf("loaded pdf file from resources: %d bytes\n",f.length());
} catch (IOException ex) {System.out.printf("loadPDF: %s\n",ex);}
} else {
System.out.printf("%s exists: %d bytes\n",destFile,f.length());
}
try {
if (f.exists()) {
f.setReadable(true);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(f);
} else {
System.out.println("Awt Desktop is not supported!");
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
String.format("%s\n",ex),
"Helpfile Problem?",
JOptionPane.ERROR_MESSAGE);
}
}
모든 Mac에서 충분히 잘 작동하지만 윈도우 7상의 IOException
을 제공이 :
java.io.IOException: Failed to open file:/C:/Program%20Files/BRC2/brc2help.pdf. Error message: Access denied.
어떤 생각이 잘못 여기서 뭐하는거야? .pdf 파일을 다른 위치로 복사하려고 시도했지만 결과는 같습니다.
필자는 비슷한 문제를 겪었으며 경로 이름의 공백과 관련이있는 것으로 보입니다. 지금 당장은 JDK에서 문제가 될 수 있다고 생각합니다. Desktop.open()에서도 발생하므로 URI 디코딩 문제 일 수 있습니다. Mac OS X에서 제대로 작동합니다. –