Codenameone 如何访问文件系统中的文件浏览器组件中的存储
我试图在browsercomponent内显示保存在FileSystemStorage中的pdf,但它在控制台上不断显示此错误:Codenameone 如何访问文件系统中的文件浏览器组件中的存储,codenameone,Codenameone,我试图在browsercomponent内显示保存在FileSystemStorage中的pdf,但它在控制台上不断显示此错误:[0813/072549.347989:INFO:console(37)]“不允许加载本地资源:file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH",资料来源:https://cn1app/streams/1 (
[0813/072549.347989:INFO:console(37)]“不允许加载本地资源:file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH",资料来源:https://cn1app/streams/1 (37)[0813/072551.123557:信息:控制台(0)]“不允许加载本地资源:file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH“,资料来源: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;
}
formhi;
集装箱装载味精;
公共屋({
表单=新表单(“显示pdf”,新边框布局());
按钮显示Pdf=新按钮(“显示Pdf”);
showPdf.addActionListener(l->{
show();
});
添加(BorderLayout.CENTER,showPdf);
form.show();
}
私人电视节目(){
hi=新表单(“PDF查看器”,new BorderLayout());
标签加载标签=新标签(“加载PDF…”);
loadingMsg=FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH,loadingMsg);
字符串pdfUrl=”https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
字符串文件名=FileSystemStorage.getInstance().getAppHomePath()+“Chapter_11”;
如果(!FileSystemStorage.getInstance().存在(文件名)){
Util.downloadUrlToFileSystemInBackground(pdfUrl,文件名);
};
嗨,addShowListener(l->{
run1(文件名);
});
嗨,show();
}
私有void run1(字符串文件名){
BrowserComponent browser=新建BrowserComponent();
setPage(getPdfViewerHtml(文件名),null);
hi.add(BorderLayout.CENTER,浏览器);
loadingMsg.remove();
hi.revalidate();
}
私有字符串getPdfViewerHtml(字符串文件名){
字符串html=“\n”
+“\n”
+“\n”
+“PDF查看器\n”
+“\n”
+“html{\n”
+“高度:100%;\n”
+“填充:0;\n”
+“}\n”
+“正文{\n”
+“高度:100%;\n”
+“溢出-y:隐藏;\n”
+“位置:固定;\n”
+“宽度:100%;\n”
+“填充:0;\n”
+“页边距:0;\n”
+“}\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“此浏览器不支持PDF!\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+ "";
返回html;
}
因此,在方法
stringgetpdfviewerhtml(stringfilename)
中,当我用URL替换fileName
时,一切正常。但我希望它显示文件系统存储中的文件。下面的代码应该正确支持本地嵌入标记:
private Container loadingMsg;
公开作废开始(){
如果(当前!=null){
current.show();
返回;
}
home();
}
公共屋({
表单=新表单(“显示pdf”,新边框布局());
按钮显示Pdf=新按钮(“显示Pdf”);
showPdf.addActionListener(l->{
showPdf.setText(“下载PDF…”);
showPdf.setEnabled(false);
form.revalidate();
show();
});
添加(BorderLayout.CENTER,showPdf);
form.show();
}
私人电视节目(){
字符串root=getAppHomePath()+“httpdocs/”;
mkdir(根);
hi=新表单(“PDF查看器”,new BorderLayout());
标签加载标签=新标签(“加载PDF…”);
loadingMsg=FlowLayout.encloseCenter(loadingLabel);
hi.add(BorderLayout.NORTH,loadingMsg);
字符串pdfUrl=”https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
字符串fileName=root+“/Chapter_11.pdf”;
如果(!FileSystemStorage.getInstance().存在(文件名)){
Util.downloadUrlToFile(pdfUrl,文件名,false);
试一试{
run1(docRoot,第11章pdf);
}捕获(IOEX异常){
Log.e(ex);
}
}否则{
试一试{
run1(docRoot,文件名);
}捕获(IOEX异常){
Log.e(ex);
}
}
嗨,show();
}
私有void run1(字符串docRoot,字符串文件名)引发IOException{
BrowserComponent browser=新建BrowserComponent();
字符串localUrl=fileName;
字符串htmlPage=getPdfViewerHtml(localUrl);
File indexHtml=新文件(docRoot,“index.html”);
writeStringToFile(indexHtml、htmlPage);
setURL(docRoot+“/index.html”);
hi.add(BorderLayout.CENTER,浏览器);
loadingMsg.remove();
hi.revalidate();
}
私有字符串getPdfViewerHtml(字符串文件名){
字符串html=“\n”
+“\n”
+“\n”
+“PDF查看器\n”
+“\n”
+“html{\n”
+“高度:100%;\n”
+“填充:0;\n”
+“}\n”
+“正文{\n”
+“高度:100%;\n”
+“溢出-y:隐藏;\n”
+“位置:固定;\n”
+“宽度:100%;\n”
+“填充:0;\n”
+“页边距:0;\n”
+“}\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”