java - Vaadin Upload Component output stream encoding issue? -


forgive me if has been discussed in forum have been looking answers problem.

i may not understand how upload component working. plan save file server can later read contents of table or text area.

this receive upload file method, writing file , returning fileoutputstream.

   public outputstream receiveupload(string filename, string mimetype) {             // create upload stream             fileoutputstream fos = null; // stream write             try {                 // open file 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 output stream write         } 

this code once 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 works , outputs contents of file, eg pdf or text file, contents wrapped odd encoding such

{\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 original file held

hi there

bye

what doing include metadata etc?

also id note added standardcharset.utf8 input stream in hope fix this, exact same without including this.

it appears file not text file, pdf file. in uploadfinished() method, first test file type using https://docs.oracle.com/javase/7/docs/api/java/nio/file/files.html#probecontenttype(java.nio.file.path). if file pdf, can use pdfbox (how read pdf files using java?) read content, or if plain text, can read 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();                 }  } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -