以下の更新に関する質問を参照してください (上の質問ではありません)。
この関数を使用して、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;
}