I’ve got multiple pages that allow the download of the same resource (retrieved from my DB).
The problem is that the download works just on some of them, even with the SAME code and calling the SAME bean.
This thing is getting quite annoying because, on the non working pages, clicking on the download link will just reload the page without any message/exception, so I can’t find out what’s happening.
Here’s my BEAN code :
package ManagedBeans;
import ejb.DispensaManagerLocal;
import entity.Dispensa;
import entity.Utente;
import java.io.ByteArrayInputStream;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RateEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
/**
*
* @author stefano
*/
@ManagedBean
@RequestScoped
public class DispensaBean {
@EJB
private DispensaManagerLocal dispensaManager;
@ManagedProperty(value = "#{loginBean.utente}")
private Utente utente;
public Utente getUtente() {
return utente;
}
public void setUtente(Utente utente) {
this.utente = utente;
}
/**
* Creates a new instance of DispensaBean
*/
public DispensaBean() {
}
public StreamedContent getDownload() {
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("dispensaId");
System.out.println("________" + id);
Dispensa d = dispensaManager.findById(Integer.parseInt(id));
String type = getMimeFromByte(d.getDatiFile());
String estensione = "";
if(type.equals("application/pdf")){
estensione = ".pdf";
} else if(type.equals("application/zip")) {
estensione = ".zip";
} else if(type.equals("application/vnd.ms-powerpoint")) {
estensione = ".ppt";
}
return new DefaultStreamedContent(new ByteArrayInputStream(d.getDatiFile()), type, d.getTitolo() + estensione);
}
private String getMimeFromByte(byte[] src) {
if (src[0] == 0x25 && src[1] == 0x50 && src[2] == 0x44 && src[3] == 0x46) {
return "application/pdf";
}
if (src[0] == 0x50 && src[1] == 0x4b) {
return "application/zip";
}
if (src[0] == 0xd0 && src[1] == 0xcf && src[2] == 0x11 && src[3] == 0xe0 && src[4] == 0xa1 && src[5] == 0xb1 && src[6] == 0x1a && src[7] == 0xe1) {
return "application/vnd.ms-powerpoint";
}
return "application/octet-stream";
}
}
Now, on the NON working pages, the getDownload() method is NOT called, as it doesn’t print anything.
Here’s the download button code
<h:form style="float: right">
<pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}">
<pou:graphicImage value="./resources/images/download.png" height="30"/>
<pou:fileDownload value="#{dispensaBean.getDownload()}"/>
<f:param name="dispensaId" value="#{dispensa.id}"/>
</pou:commandLink>
</h:form>
What I’ve noticed is that the download link just RELOADS the page instead of calling the method, and this happens only in the pages in which #{dispensa.id} depends on a GET parameter.
For example, I’ve got a page called dispensa.xhtml that displays all my files in the DB if no GET parameters are passed.
Indeed, dispensa.xhtml?id=5 will display just the file with id=5.
Clicking on the download link, in the first case, works without problems.
Doing it in the second case will reload the page and will lose the GET parameter, so the it will load dispensa.xhtml instead of dispensa.xhtml?id=5.
I’d think that there’s some problem in using the GET parameter, but..yesterday it WORKED and I did NOT change this code!
The other NON working page is ricerca.xhtml which shows the (multiple) results of a query given by ricerca.xhtml?key=query.
Finally, to mess things up, the download in profile.xhtml?user=username WORKS.
This destroys my whole theory about GET parameters.
To avoid having a null byte[] datiFile, I’ved edited my Dispensa entity this way :
@Basic(optional = true, fetch=FetchType.EAGER)
@Lob
@Column(name = "datiFile")
private byte[] datiFile;
I don’t know what to do because it doesn’t say what’s going wrong, it just reloads the page, bypassing my download!
EDIT :
I’ve tried changing my getDownload() method to return a File which is on my HD, to understand if the problem is caused by a null data on the db but it still doesn’t work as I said!
Seems that I solved this by using an alternative solution.
I’ve changed all
to
where
download.xhtmlhas this code :So it loads the download page, autoclicks on the download link and it autocloses the page when the download dialog is shown.