0
當我嘗試使用ice:outputResource從服務器(Liferay jsf2.0)下載文件時,我遇到了一些奇怪的麻煩。Icefaces outputresource返回錯誤500,直到項目重建
使用相同的代碼,頁面和文件,但outputresource會拋出錯誤代碼500,直到我重建我的項目,重建後我的文件可以下載。
我的場景是(我敢肯定,路徑和文件存在):
用戶上傳文件A(在臨時存儲文件) - 下載OK
用戶攢動(移動文件到資源文件夾)
用戶編輯表單(從資源文件夾重裝文件) - 下載失敗(拋出錯誤500)
重建代碼,然後上傳另一個文件B(同A)
用戶編輯表單(重載A和B) - 下載一個確定的,但下載B失敗
我把資源初始化代碼內(休眠)模式,以避免循環:
@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;
}
種源代碼
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}"/>