0

forgive me if this has been discussed in the forum but I have been looking for answers to my problem.

I may not fully understand how the upload component is working. I plan to save a file to my server that I can later read the contents of into a table or text area.

This is my receive upload file method, where I am writing to a File and returning the FileOutputStream.

   public OutputStream receiveUpload(String filename, String mimeType) {
            // Create upload stream
            FileOutputStream fos = null; // Stream to write to
            try {
                // Open the file for writing.
                outputFile = new File("/tmp/" + filename);
                fos = new FileOutputStream(outputFile);
            } catch (final java.io.FileNotFoundException e) {
                new Notification("Could not open file<br/>",
                        e.getMessage(),
                        Notification.Type.ERROR_MESSAGE)
                .show(Page.getCurrent());
                return null;
            }
            return fos; // Return the output stream to write to
        }

This is my code once the upload succeeds

public void uploadFinished(Upload.FinishedEvent finishedEvent) {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()), StandardCharsets.UTF_8));
                    String line;
                    while ((line = reader.readLine()) != null)
                    {
                        textArea.setValue(textArea.getValue() + "\n" + line);
                    }
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

This all works and outputs the contents of a file, eg PDF or Text file, but the contents are all wrapped with odd encoding such as

{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 hi there\ \ bye}

where the original file held

hi there

bye

What am I doing to include all the metadata etc?

Also Id like to note I added the standardcharset.UTF8 to the input stream in hope to fix this, but it is the exact same as without including this.

jimmyboix
  • 117
  • 1
  • 8
  • It appears that you file you are reading is not a plain text file (as you mentioned PDF), to read the content of PDF or Word doc you can use some specific library to read the content. – ohw Jan 16 '16 at 22:57
  • thanks for this, Ive had a bit of an oversight here. I'll try a package like pdfbox. – jimmyboix Jan 17 '16 at 12:04

1 Answers1

0

It appears the file is not a text file, but a PDF file. In your uploadFinished() method, you could first test the file type using https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType(java.nio.file.Path). If the file is a PDF, you can use PDFBox (How to read PDF files using Java?) to read the content, or if it is plain text, you can read it as you already are.

import java.nio.file.Files;
import java.nio.file.Path;

...

String contentType = Files.probeContentType(outputFile.toPath());
if(contentType.equals("application/pdf"))
{
       PDDocument document = null; 
    document = PDDocument.load(outputFile);
    document.getClass();
    if( !document.isEncrypted() ){
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        stripper.setSortByPosition( true );
        PDFTextStripper Tstripper = new PDFTextStripper();
        String st = Tstripper.getText(document);
        textArea.setValue(st);
    }
    }catch(Exception e){
        e.printStackTrace();
    }

}
else if(contentType.equals("text/plain"))
{
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()), StandardCharsets.UTF_8));
                    String line;
                    while ((line = reader.readLine()) != null)
                    {
                        textArea.setValue(textArea.getValue() + "\n" + line);
                    }
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

}
Community
  • 1
  • 1
AdeelMufti
  • 341
  • 1
  • 8