【发布时间】:2016-04-24 03:41:23
【问题描述】:
我正在开发 Java 8、JSF 2、Primefaces 5.1。
与 PDF 或 Docx 的对话有效,但当我显示文件名时,它只是跳过 UTF-8 编码的字母,在我的情况下,是立陶宛字母,如 ą,č,ę,ė,į,š,ų,ū
我尝试过的农场是:
<h:form enctype="multipart/form-data;charset=UTF-8">
Charset.forName("UTF-8").encode(myString)
or
byte[] bytes = templateTitle.getBytes(Charset.forName("UTF-8"));
String title = new String(bytes, Charset.forName("UTF-8"));
or
UTF-8 text is garbled when form is posted as multipart/form-data
查了一些关于编码的教程,还是没用,
也检查了这个,但我只是不明白这个例子...... Primefaces fileDownload non-english file names corrupt
my code:
Download file as docx
public void downloadTemplateAsDocx() throws Exception {
try {
InputStream content = null;
String objID = this.actData.getMainActs().get(0).getId();
ContentStream cmisStream = folderCatalogue.getDocumentContentStream(objID);
content = cmisStream.getStream();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(content);
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
File fileTmp = File.createTempFile("tempDocFile", "docx");
wordMLPackage.save(fileTmp);
streamedContent = new DefaultStreamedContent(new FileInputStream(fileTmp), cmisStream.getMimeType(),
templateTitle + ".docx", "UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException eInv) {
eInv.printStackTrace();
} catch (IOException ioEx) {
ioEx.printStackTrace();
} catch (Docx4JException docxEx) {
docxEx.printStackTrace();
}
}
.Pdf 文件下载代码。
public void downloadTemplateAsPdf() {
try {
InputStream content = null;
String objID = this.actData.getMainActs().get(0).getId();
ContentStream cmisStream = folderCatalogue.getDocumentContentStream(objID);
content = cmisStream.getStream();
File fileTmp = File.createTempFile("tempFile", "pdf");
OutputStream fileStream = new FileOutputStream(fileTmp);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fileStream);
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
worker.parseXHtml(writer, document, content, Charset.forName("UTF-8"));
document.close();
fileStream.close();
streamedContent = new DefaultStreamedContent(new FileInputStream(fileTmp), cmisStream.getMimeType(),
templateTitle + ".pdf");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File was not found");
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception exeption) {
exeption.printStackTrace();
}
}
编辑:
<p:fileDownload value="#{controller.streamedContent}" />
private StreamedContent streamedContent;
【问题讨论】:
-
露天相关吗?它与 apdf 或 docx 有关吗?或者具有 utf-8 文件名的纯文本文件是否失败?请参阅minimal reproducible example 和 http://www.stackoverflow.com/tags/jsf/info。您对提到的问题有什么不明白的地方?在那里要求澄清
-
对不起,忘了写关于露天的。 Alfresco 方面工作正常,文件保存为纯文本,流转换为 pdf 或 docx,但我确信问题不是来自 alfresco,
-
所以从问题中删除所有这些。保持简洁
-
我的评论还有很多内容。那么一个非常简单的字符串转换为输入流并以 utf-8 名称下载有效吗?你在哪里
p:download -
阅读 [mcve) 和 stackoverflow.com/tags/jsf/info(再次?)
标签: java jsf-2 primefaces utf-8