I have several pages that allow you to load the same resource (extracted from my database).
The problem is that the download only works on some of them, even with the SAME code and the calling SAME bean.
This thing becomes quite annoying because on non-working pages, clicking on the download link will simply reload the page without any messages / exceptions, so I cannot find out what is happening.
Here is 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;
@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;
}
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 work pages, the method is getDownload()NOT called because it does not print anything.
Here is 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>
, , , #{dispensa.id} GET.
, dispensa.xhtml, , GET.
, dispensa.xhtml?id=5 id = 5.
, .
GET, dispensa.xhtml dispensa.xhtml?id=5.
, - GET, ... , !
NON ricerca.xhtml, () , ricerca.xhtml?key=query.
, , profile.xhtml?user=username WORKS.
GET.
byte[] datiFile, Dispensa :
@Basic(optional = true, fetch=FetchType.EAGER)
@Lob
@Column(name = "datiFile")
private byte[] datiFile;
, , , , , !
:
getDownload(), File, HD, , db, - , !