0

以下の更新に関する質問を参照してください (上の質問ではありません)。

この関数を使用して、Liferay で任意のドキュメント タイプ (特に PDF) を開こうとしました。Awt Desktop is not supported!しかし、関数に記載されているように、私は常にメッセージを受け取ります。Awt デスクトップを有効にするにはどうすればよいですか? インターネットで検索してみましたが、何も見つかりませんでした。誰か助けてください。ありがとう。

public void viewFileByAwt(String file) {
    try {
        File File = new File(getPath(file));
        if (File.exists()) {
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(File);
            } else {
                System.out.println("Awt Desktop is not supported!");
            }
        } else {
            //File is not exists
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

ソース: http://www.mkyong.com/java/how-to-open-a-pdf-file-in-Java/


アップデート

以下のコードを見るとわかるように、両方のモード (1ダウンロード2用とプレビュー用) はかなりうまく機能していますが、残念ながら 2 番目のモード (プレビュー モード) は PDF でしか機能しません。

ここで私がやりたいことは、ユーザーがpreviewボタンをクリックしているときに、PDF 以外のファイル (拡張子のみに制限: DOC、DOCX、XLS、XLSX、ODT、ODS ) を最初に PDF に変換してから、ブラウザーに表示する必要があることです。以下のコードで説明したのと同じ方法で。それは可能ですか?関数にすべてのコンバーターを配置するのが難しすぎる場合は、個別の関数では各拡張機能で問題ありません。

public StreamedContent getFileSelected(final StreamedContent doc, int mode) throws Exception {
    //Mode: 1-download, 2-preview
    try {
        File localfile = new File(getPath(doc.getName()));
        FileInputStream fis = new FileInputStream(localfile);
        if (mode == 2 && !(doc.getName().substring(doc.getName().lastIndexOf(".") + 1)).matches("pdf")) {
            localfile = DocumentConversionUtil.convert(doc.getName(), fis, doc.getName().substring(doc.getName().lastIndexOf(".") + 1), "pdf");
            fis = new FileInputStream(localfile.getPath());
        }

        if (localfile.exists()) {
            try {
                PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
                if (mode == 1)      res.setHeader("Content-Disposition", "attachment; filename=\"" + doc.getName() + "\"");
                else if (mode == 2) res.setHeader("Content-Disposition", "inline; filename=\"" + doc.getName() + "\"");
                res.setHeader("Content-Transfer-Encoding", "binary");
                res.setContentType(getMimeType(localfile.getName().substring(localfile.getName().lastIndexOf(".") + 1)));
                res.flushBuffer();
                OutputStream out = res.getOutputStream();
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                    buffer = new byte[4096];
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}
4

2 に答える 2

2

Liferay はポータル サーバーです。そのユーザー インターフェイスはブラウザーで実行されます。AWT は、デスクトップ UI の Java 1.0 ベースです。

AWT はそれを表示する方法ではないと思います。

ファイルを開いて、application/pdf MIME タイプを使用してバイトをポートレットにストリーミングできないのはなぜですか?

于 2013-06-15T02:26:12.050 に答える
1

最初にマシンにopenofficeをインストールする必要があります http://www.liferay.com/documentation/liferay-portal/6.1/user-guide/-/ai/openoffice

liferay で openoffice を設定した後、liferay の DocumentConversionUtil クラスを使用してドキュメントを変換できます。

DocumentConversionUtil.convert(String id, InputStream is, String sourceExtension,String targetExtension)

上記のコードは入力ストリームを返します。この変換後、ブラウザで pdf を表示できます。

于 2013-06-17T06:06:22.690 に答える