I am writing an application in which I use the upload component. The intention is that the user can upload any file type to a cloud in this way. So these files are stored on the cloud. At the moment this certainly works for .txt and Excel files. However, for quite a few other files such as pdf, word, … this causes problems. For example with PDF files when I open the file I get a message that the file is damaged. Isn’t there a clean way to get any file type from the upload component and save it in a directory?
@Override
public OutputStream receiveUpload(String fileName, String mimeType) {
try {
file = new File("/home/yroot/" + fileName);
return new FileOutputStream(file);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public VerticalLayout vertiDragAndDrop(){
MemoryBuffer buffer = new MemoryBuffer();
Upload upload = new Upload(buffer);
upload.addSucceededListener(event -> {
InputStream inputStream = buffer.getInputStream();
FileData fileData =buffer.getFileData();
fileData.getFileName();
OutputStream outp = receiveUpload(fileData.getFileName(), fileData.getMimeType());
try {
File file = new File("/home/yroot/output/"+fileData.getFileName());
FileOutputStream fop = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
System.out.println("CREATE FILE");
}
byte [] contentInBytes = fileData.getOutputBuffer().toString().getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
});
return new VerticalLayout(upload, new Label("Drag And Drop"));
}
I can upload txt files without any problem and they open with the correct content.
However with pdf, word and several picture filetypes I get to see that the files are damaged.
Check the attachment for more information.