Andre
Andre

Reputation: 1

Show image from string path outside the project Primefaces

I have String path that I'll use to show image on header, I take the path from database that return a String filename for image, then the image are in "D:\Job\Potapota\images\(filename)". I already did the String path from db that I initialize on a.setValue(dao.getLogos());. It means if i want to get the filename only like a.getValue().

so this is my html page

<p:graphicImage value="#{fileUploadViewBean.imageStatus}" library="atlantis-layout"/>

then the class FileUploadViewBean is this

public class FileUploadViewBean extends CommonBean {

private StreamedContent imageStatus;
public void init() throws IOException{
    getLogo();
}

public StreamedContent getImageStatus() {
    return imageStatus;
}

public void setImageStatus(StreamedContent imageStatus) {
    this.imageStatus = imageStatus;
}

public void getLogo() throws IOException {
    SystemMaster a = new SystemMaster();
    FacesContext context = FacesContext.getCurrentInstance();
    try {
        a.setValue(dao.getLogos());
    } catch (Exception e) {
        setMessage(new FacesMessage(e.getMessage()));
        getMessage().setSeverity(FacesMessage.SEVERITY_FATAL);
        FacesContext.getCurrentInstance().addMessage(null, getMessage());
    }
    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so
        // that it will generate right URL.
        imageStatus = new DefaultStreamedContent();
    } else {
        // So, browser is requesting the image. Return a real
        // StreamedContent with the image bytes.
        String filename = 
context.getExternalContext().getRequestParameterMap().get(a.getValue());
        imageStatus = new DefaultStreamedContent(
                new FileInputStream(new File("D:\\Job\\Potapota\\images\\", 
        filename)), "image/jpeg");
    }
}

but then the image doesn't show up, can somebody know what is the true code for this.

Upvotes: 0

Views: 28

Answers (0)

Related Questions