如何在浏览器组件中访问FileSystemStorage中的文件

问题描述

我正在尝试在浏览器组件中显示保存在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)中,当我用网址替换fileName时,一切正常。但是我希望它显示FileSystemStorage中的文件

解决方法

以下代码应正确支持本地embed标签:

    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);
        }
        
    }

}