-1

I am working on Java 8, JSF 2, Primefaces 5.1.

Conversation to PDF or Docx works, but when I am displaying file name, it just skips UTF-8 encoded letters, in my case, Lithuanian letters like ą,č,ę,ė,į,š,ų,ū

What I have tried so farm is :

<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

checked some tuttorials about encoding, still, no use,

also checked this, but I just do not understand this example... 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();
        }

    }

code for .Pdf file download.

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

EDIT:

<p:fileDownload  value="#{controller.streamedContent}" />

private StreamedContent streamedContent;
Community
  • 1
  • 1
DevyDev
  • 846
  • 3
  • 14
  • 37
  • Is alfresco relevant? Is it relevant it is apdfor docx? Or does a plain text file with an utf-8 filename fail to? See [mcve] andhttp://www.stackoverflow.com/tags/jsf/info. And what do you not understand about the referred issue? Ask clarification there – Kukeltje Jan 18 '16 at 20:57
  • Sorry, forgot to write about alfresco. Alfresco side works fine, file is saved as plain/text, and stream is converted to pdf or docx, but I am sure that problem is coming not from alfresco, – DevyDev Jan 18 '16 at 21:00
  • So remove al that from the question. Keep it clean and simple – Kukeltje Jan 18 '16 at 21:00
  • There was a lot more in my comment. So a very simple string converted to an inputstream and downloaded with an utf-8 name works? And where is your `p:download` – Kukeltje Jan 18 '16 at 21:10
  • I am trying to keep it simple, just 2 methods I am using, and what I have tried so far. p.s. Edited post – DevyDev Jan 18 '16 at 21:15
  • 1
    Read [mcve) and http://www.stackoverflow.com/tags/jsf/info (again?) – Kukeltje Jan 18 '16 at 21:21

1 Answers1

-1

Solution,

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

This Encoding works fine, just it replaces all spaces to + that's why I loop over it.

DevyDev
  • 846
  • 3
  • 14
  • 37