Question

When i try to use ice:outputResource to download file from server (Liferay jsf2.0), i meet some strange trouble.

With the same code, and page, and file, but outputresource will throw error code 500 until i rebuild my project, after rebuild my file can be downloaded.

My scene is(i'm sure that path and file existed):

User upload file A (store file in temp) - downloaded ok

User save action (move file to resource folder)

User edit form (reload file A from resource folder) - download fail (throw error 500)

rebuild code, then upload another file B (same as A)

User edit form (reload A and B) - download A OK but download B fail

I put resource init code inside (Hibernate)model to avoid loop:

@Transient
    private Resource ksf_resource;

    public Resource getKsf_resource() {
        if(ksf_resource == null){
            try{                    
                ksf_resource = new CCHCResource("/"+path + "/" , ksf_realname);
                _log.info("path {}, file {}", path, ksf_realname);
                }

            }catch(Exception ex){
                //file not found or server error
                _log.warn("File not found!");
                ksf_resource = null;
            }
        }
        return ksf_resource;
    }

resouce code

public class CCHCResource implements Resource, Serializable {

    private static final long serialVersionUID = -639586497927876085L;


    private String path;
    private String resourceName;
    private InputStream inputStream;
    private final Date lastModified;

    public CCHCResource(String path, String resourceName) {
        this.path = path;
        this.resourceName = resourceName;
        this.lastModified= new Date();
    }

    @Override
    public InputStream open() throws IOException {
        InputStream stream = FacesContext.getCurrentInstance()
                .getExternalContext().getResourceAsStream(path + resourceName);
//      System.out.println("get resource: " + path + resourceName);
        byte[] byteArray = toByteArray(stream);
        inputStream = new ByteArrayInputStream(byteArray);
        return inputStream;
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len = 0;
        while ((len = input.read(buf)) > -1) output.write(buf, 0, len);
        return output.toByteArray();
    }

    @Override
    public String calculateDigest() {
       return resourceName;
    }

    @Override
    public void withOptions(Options arg0) throws IOException {
    }

    @Override
    public Date lastModified() {
        return lastModified;
    }
}

xhtml

<ice:outputResource
    image="#{resource['images:isoDownload']}"       
    resource="#{file.ksf_resource}"
    attachment="true"
    fileName="#{file.ksf_displayname}"
    type="application/text"
    label="#{itemtp.ksf_displayname}"/>
Was it helpful?

Solution

Don't really know why, but add attribute shared="false" can resolve this error

<ice:outputResource
    image="#{resource['images:isoDownload']}"       
    resource="#{file.ksf_resource}"
    attachment="true"
    fileName="#{file.ksf_displayname}"
    type="application/text"
    label="#{itemtp.ksf_displayname}"
shared="false"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top