Problem with p:fileDownload migration to PF10.

UI Components for JSF
Post Reply
Dan.Belu
Posts: 3
Joined: 28 Oct 2018, 14:23

23 Jul 2021, 21:05

As I'm trying to migrate a project from PF 3.4.2 to PF10 I'm enountering a problem with the fileDowload retrieving a corrupt file.

Previous code PF 3.4

Code: Select all

<p:commandLink id="file_download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)">
    <p:fileDownload value="#{fileDownloadController.downloadFile}"/>
</p:commandLink>

@ManagedBean(name = "fileDownloadController")
@ViewScoped
public class FileDownloadController implements Serializable{

	public StreamedContent getDownloadFile() {
		downloadFile = newDownloadFile();
		return downloadFile;
	}

	private StreamedContent newDownloadFile() {
		InputStream downloadStream = new ByteArrayInputStream(... fetching a BLOB from Oracle db as byte[] ...);
		// contentType = "application/pdf", fileName = "somefile.pdf"
		// The file is not a physical file it's only as IntputStream
		downloadF = new DefaultStreamedContent(downloadStream, contentType, fileName);                    
		try {                        
			downloadStream.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}	
		return downloadF;
	}
}
Current code PF 10

Trying to use Spring Boot that's why the Controller annotations are from that framework.
All other PF functionality works ok with Spring Boot.
The page controller scope is @Scope(value = ViewScope.SCOPE_VIEW), where ViewScope is a custom class which brings javax.faces.context.FacesContext.getCurrentInstance().getViewRoot().getViewMap() into the application.

Code: Select all

<p:commandLink id="file_download">
	<p:fileDownload value="#{fileDownloadController.getDownloadFile(controller.note)}"/>
</p:commandLink> 

@Scope(value= WebApplicationContext.SCOPE_REQUEST)
@Component(value = "fileDownloadController") // Name of the bean in Spring IoC
@ELBeanName(value = "fileDownloadController") // Name of the bean used by JSF
public class FileDownloadController {

    public StreamedContent getDownloadFile(Note note) {
        downloadFile = newDownloadFile(note);
        return downloadFile;
    }

    private StreamedContent newDownloadFile(Note note) {
        StreamedContent downloadF = null;
            // contentType = "application/pdf", fileName = "somefile.pdf"
            byte[] attachment = ... fetching a BLOB from Oracle db as byte[] ...;
            downloadF = DefaultStreamedContent.builder()
                    .name(fileName)
                    .contentType(contentType)
                    .stream(() -> new ByteArrayInputStream(attachment))
                    .build();        
        return downloadF;
    }
	
}	

The problem

The resulting downloadF seems to be corrupt.
Firefox does not recognize its type and assumes it's a json file when trying to open it directly.
When saved to the disk it cannot be opened due to "it is either an not supportted file type or because the file has been damaged" .
Other browsers react in a similar manner.

I tried to play with the DefaultStreamedContent.setContentEncoding but there is no clear direction on that, and I got nowhere.

Any help would be greatly appreciated.
PrimeFaces 10.0.0
JSF 2.1.7
Oracle WebLogic server 12.2.1.4.0
Oracle 11g database

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

26 Jul 2021, 14:12

Don't close your stream maybe. Leave it open and let the download close it?

Code: Select all

public FileDownloadView() {
        file = DefaultStreamedContent.builder()
                .name("downloaded_boromir.jpg")
                .contentType("image/jpg")
                .stream(() -> FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/demo/images/boromir.jpg"))
                .build();
    }
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Dan.Belu
Posts: 3
Joined: 28 Oct 2018, 14:23

26 Jul 2021, 17:59

Thank you Melloware,

I'm not closing the stream in PF10. The stream.close() code is from the PF 3.4.2 version which works fine.
PrimeFaces 10.0.0
JSF 2.1.7
Oracle WebLogic server 12.2.1.4.0
Oracle 11g database

Dan.Belu
Posts: 3
Joined: 28 Oct 2018, 14:23

26 Jul 2021, 18:04

I think this small change in code solved the problem

Code: Select all

byte[] attachment = = ... fetching a BLOB from Oracle db as byte[] ...;
final InputStream is = new ByteArrayInputStream(attachment);
downloadF = DefaultStreamedContent.builder()
                    .name(fileName)
                    .contentType(contentType)
                    .stream(() -> is).build();
... moving the ByteArrayInputSream into a final InputSream.
PrimeFaces 10.0.0
JSF 2.1.7
Oracle WebLogic server 12.2.1.4.0
Oracle 11g database

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

26 Jul 2021, 22:06

Nice!
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests