Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix file upload #852

Merged
merged 2 commits into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/main/java/org/joinfaces/example/view/FileMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.joinfaces.example.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;

Expand All @@ -29,12 +30,12 @@
import org.primefaces.model.file.UploadedFile;

import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

/**
* FileMBean to test primefaces upload component.
* @author Marcelo Fernandes
*/
@SuppressFBWarnings("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION")
@Component
@ViewScoped
public class FileMBean implements Serializable {
Expand All @@ -52,17 +53,11 @@ public class FileMBean implements Serializable {
/**
* Upload file action.
*/
public void upload() {
public void upload() throws IOException {
if (this.uploadedFile != null) {
byte[] data = FileCopyUtils.copyToByteArray(this.uploadedFile.getInputStream());
this.downloadFile = DefaultStreamedContent.builder()
.stream(() -> {
try {
return this.uploadedFile.getInputStream();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.stream(() -> new ByteArrayInputStream(data))
.contentType(this.uploadedFile.getContentType())
.name(this.uploadedFile.getFileName())
.build();
Expand Down