JSF Primefaces p:fileDownload文件名包含UTF-8字符

时间:2016-01-18 20:41:18

标签: java jsf-2 primefaces utf-8

我正在研究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

检查了一些关于编码的tuttorials,仍然没有用,

也检查过这个,但我只是不明白这个例子...... 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;

1 个答案:

答案 0 :(得分:-1)

解,

String title = URLEncoder.encode(templateTitle, "UTF-8");
            StringBuilder fileName = new StringBuilder(title);

            if (title.contains("+")) {
            for (int i = 0; i < title.length(); i++) {
                if (title.charAt(i) == '+') {
                        fileName.setCharAt(i, ' ');
                    }
                }
            }

这个编码工作正常,只是它将所有空格替换为+为什么我循环它。