如何在 Browser 组件中访问 FileSystemStorage 中的文件
How to access files in FileSystemStorage in Browser component
我试图在浏览器组件内显示保存在 FileSystemStorage 中的 pdf,但它一直在控制台上给我这个错误:[0813/072549.347989:INFO:CONSOLE(37)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (37) [0813/072551.123557:INFO:CONSOLE(0)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (0)
。好像有一个我需要解决的限制。我该如何解决这个问题?
编辑...在下面添加了信息。
这是我使用的代码:
Form hi;
Container loadingMsg;
public void Home(){
Form form = new Form("Display pdf", new BorderLayout());
Button showPdf = new Button("Show Pdf");
showPdf.addActionListener(l->{
show();
});
form.add(BorderLayout.CENTER, showPdf);
form.show();
}
private void show(){
hi = new Form("PDF Viewer", new BorderLayout());
Label loadingLabel = new Label("Loading PDF...");
loadingMsg = FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH, loadingMsg);
String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
String fileName = FileSystemStorage.getInstance().getAppHomePath() + "Chapter_11";
if (!FileSystemStorage.getInstance().exists(fileName)) {
Util.downloadUrlToFileSystemInBackground(pdfUrl, fileName);
};
hi.addShowListener(l -> {
run1(fileName);
});
hi.show();
}
private void run1(String fileName) {
BrowserComponent browser = new BrowserComponent();
browser.setPage(getPdfViewerHtml(fileName), null);
hi.add(BorderLayout.CENTER, browser);
loadingMsg.remove();
hi.revalidate();
}
private String getPdfViewerHtml(String fileName) {
String html = "<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>PDF Viewer</title>\n"
+ " <style>\n"
+ " html{\n"
+ " height: 100%;\n"
+ " padding: 0;\n"
+ " }\n"
+ " body{\n"
+ " height: 100%;\n"
+ " overflow-y: hidden;\n"
+ " position: fixed;\n"
+ " width: 100%;\n"
+ " padding: 0;\n"
+ " margin: 0;\n"
+ " }\n"
+ " </style>\n"
+ " </head>\n"
+ " <body>\n"
+ "\n"
+ " <div style= \"height: 100%; margin: 0;\">\n"
+ " <iframe\n"
+ " src='"+fileName+"#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
+ " width=\"100%\"\n"
+ " height=\"100%\"\n"
+ " >\n"
+ " <p>This browser does not support PDF!</p>\n"
+ " </iframe>\n"
+ "\n"
+ " </div>\n"
+ "\n"
+ " </body>\n"
+ "</html>";
return html;
}
所以在方法 String getPdfViewerHtml(String fileName)
中,当我用 URL 替换 fileName
时,一切正常。但我希望它显示来自 FileSystemStorage 的文件。
下面的代码应该正确支持本地嵌入标签:
private Container loadingMsg;
public void start() {
if (current != null) {
current.show();
return;
}
home();
}
public void home() {
Form form = new Form("Display pdf", new BorderLayout());
Button showPdf = new Button("Show Pdf");
showPdf.addActionListener(l -> {
showPdf.setText("Dowloading PDF...");
showPdf.setEnabled(false);
form.revalidate();
show();
});
form.add(BorderLayout.CENTER, showPdf);
form.show();
}
private void show() {
String root = getAppHomePath() + "httpdocs/";
mkdir(root);
hi = new Form("PDF Viewer", new BorderLayout());
Label loadingLabel = new Label("Loading PDF...");
loadingMsg = FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH, loadingMsg);
String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
String fileName = root + "/Chapter_11.pdf";
if (!FileSystemStorage.getInstance().exists(fileName)) {
Util.downloadUrlToFile(pdfUrl, fileName, false);
try {
run1(docRoot, "Chapter_11.pdf");
} catch (IOException ex) {
Log.e(ex);
}
} else {
try {
run1(docRoot, fileName);
} catch (IOException ex) {
Log.e(ex);
}
}
hi.show();
}
private void run1(String docRoot, String fileName) throws IOException {
BrowserComponent browser = new BrowserComponent();
String localUrl = fileName;
String htmlPage = getPdfViewerHtml(localUrl);
File indexHtml = new File(docRoot, "index.html");
writeStringToFile(indexHtml, htmlPage);
browser.setURL(docRoot + "/index.html");
hi.add(BorderLayout.CENTER, browser);
loadingMsg.remove();
hi.revalidate();
}
private String getPdfViewerHtml(String fileName) {
String html = "<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>PDF Viewer</title>\n"
+ " <style>\n"
+ " html{\n"
+ " height: 100%;\n"
+ " padding: 0;\n"
+ " }\n"
+ " body{\n"
+ " height: 100%;\n"
+ " overflow-y: hidden;\n"
+ " position: fixed;\n"
+ " width: 100%;\n"
+ " padding: 0;\n"
+ " margin: 0;\n"
+ " }\n"
+ " </style>\n"
+ " </head>\n"
+ " <body>\n"
+ "\n"
+ " <div style= \"height: 100%; margin: 0;\">\n"
+ " <iframe\n"
+ " src='" + fileName + "#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
+ " width=\"100%\"\n"
+ " height=\"100%\"\n"
+ " >\n"
+ " <p>This browser does not support PDF!</p>\n"
+ " </iframe>\n"
+ "\n"
+ " </div>\n"
+ "\n"
+ " </body>\n"
+ "</html>";
return html;
}
private void writeStringToFile(File file, String content) throws IOException {
FileSystemStorage fs = FileSystemStorage.getInstance();
try (OutputStream os = fs.openOutputStream(file.getAbsolutePath())) {
Util.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), os);
}
}
}
我试图在浏览器组件内显示保存在 FileSystemStorage 中的 pdf,但它一直在控制台上给我这个错误:[0813/072549.347989:INFO:CONSOLE(37)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (37) [0813/072551.123557:INFO:CONSOLE(0)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (0)
。好像有一个我需要解决的限制。我该如何解决这个问题?
编辑...在下面添加了信息。
这是我使用的代码:
Form hi;
Container loadingMsg;
public void Home(){
Form form = new Form("Display pdf", new BorderLayout());
Button showPdf = new Button("Show Pdf");
showPdf.addActionListener(l->{
show();
});
form.add(BorderLayout.CENTER, showPdf);
form.show();
}
private void show(){
hi = new Form("PDF Viewer", new BorderLayout());
Label loadingLabel = new Label("Loading PDF...");
loadingMsg = FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH, loadingMsg);
String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
String fileName = FileSystemStorage.getInstance().getAppHomePath() + "Chapter_11";
if (!FileSystemStorage.getInstance().exists(fileName)) {
Util.downloadUrlToFileSystemInBackground(pdfUrl, fileName);
};
hi.addShowListener(l -> {
run1(fileName);
});
hi.show();
}
private void run1(String fileName) {
BrowserComponent browser = new BrowserComponent();
browser.setPage(getPdfViewerHtml(fileName), null);
hi.add(BorderLayout.CENTER, browser);
loadingMsg.remove();
hi.revalidate();
}
private String getPdfViewerHtml(String fileName) {
String html = "<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>PDF Viewer</title>\n"
+ " <style>\n"
+ " html{\n"
+ " height: 100%;\n"
+ " padding: 0;\n"
+ " }\n"
+ " body{\n"
+ " height: 100%;\n"
+ " overflow-y: hidden;\n"
+ " position: fixed;\n"
+ " width: 100%;\n"
+ " padding: 0;\n"
+ " margin: 0;\n"
+ " }\n"
+ " </style>\n"
+ " </head>\n"
+ " <body>\n"
+ "\n"
+ " <div style= \"height: 100%; margin: 0;\">\n"
+ " <iframe\n"
+ " src='"+fileName+"#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
+ " width=\"100%\"\n"
+ " height=\"100%\"\n"
+ " >\n"
+ " <p>This browser does not support PDF!</p>\n"
+ " </iframe>\n"
+ "\n"
+ " </div>\n"
+ "\n"
+ " </body>\n"
+ "</html>";
return html;
}
所以在方法 String getPdfViewerHtml(String fileName)
中,当我用 URL 替换 fileName
时,一切正常。但我希望它显示来自 FileSystemStorage 的文件。
下面的代码应该正确支持本地嵌入标签:
private Container loadingMsg;
public void start() {
if (current != null) {
current.show();
return;
}
home();
}
public void home() {
Form form = new Form("Display pdf", new BorderLayout());
Button showPdf = new Button("Show Pdf");
showPdf.addActionListener(l -> {
showPdf.setText("Dowloading PDF...");
showPdf.setEnabled(false);
form.revalidate();
show();
});
form.add(BorderLayout.CENTER, showPdf);
form.show();
}
private void show() {
String root = getAppHomePath() + "httpdocs/";
mkdir(root);
hi = new Form("PDF Viewer", new BorderLayout());
Label loadingLabel = new Label("Loading PDF...");
loadingMsg = FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH, loadingMsg);
String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
String fileName = root + "/Chapter_11.pdf";
if (!FileSystemStorage.getInstance().exists(fileName)) {
Util.downloadUrlToFile(pdfUrl, fileName, false);
try {
run1(docRoot, "Chapter_11.pdf");
} catch (IOException ex) {
Log.e(ex);
}
} else {
try {
run1(docRoot, fileName);
} catch (IOException ex) {
Log.e(ex);
}
}
hi.show();
}
private void run1(String docRoot, String fileName) throws IOException {
BrowserComponent browser = new BrowserComponent();
String localUrl = fileName;
String htmlPage = getPdfViewerHtml(localUrl);
File indexHtml = new File(docRoot, "index.html");
writeStringToFile(indexHtml, htmlPage);
browser.setURL(docRoot + "/index.html");
hi.add(BorderLayout.CENTER, browser);
loadingMsg.remove();
hi.revalidate();
}
private String getPdfViewerHtml(String fileName) {
String html = "<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>PDF Viewer</title>\n"
+ " <style>\n"
+ " html{\n"
+ " height: 100%;\n"
+ " padding: 0;\n"
+ " }\n"
+ " body{\n"
+ " height: 100%;\n"
+ " overflow-y: hidden;\n"
+ " position: fixed;\n"
+ " width: 100%;\n"
+ " padding: 0;\n"
+ " margin: 0;\n"
+ " }\n"
+ " </style>\n"
+ " </head>\n"
+ " <body>\n"
+ "\n"
+ " <div style= \"height: 100%; margin: 0;\">\n"
+ " <iframe\n"
+ " src='" + fileName + "#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
+ " width=\"100%\"\n"
+ " height=\"100%\"\n"
+ " >\n"
+ " <p>This browser does not support PDF!</p>\n"
+ " </iframe>\n"
+ "\n"
+ " </div>\n"
+ "\n"
+ " </body>\n"
+ "</html>";
return html;
}
private void writeStringToFile(File file, String content) throws IOException {
FileSystemStorage fs = FileSystemStorage.getInstance();
try (OutputStream os = fs.openOutputStream(file.getAbsolutePath())) {
Util.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), os);
}
}
}