下载文件未显示liferay portlet中的总文件大小

时间:2014-06-17 09:25:31

标签: java java-ee download liferay-6

我正在使用serveResource来允许通过我的portlet下载文档库文件。

但是,它只显示下载的大小,但不显示要下载的大小。

String mimetype = doc_file.getMimeType(); //doc_file is fileentry from document library       

if (mimetype == null) {           
    mimetype = "application/octet-stream";       
}
res.setContentType(mimetype); // res is ResourceResponse

res.addProperty(HttpHeaders.CONTENT_DISPOSITION, 
                " filename=\"" + 
                DocumentUtil.docNamingConvention(doc_file.getTitle(), "get") + 
                "." + doc_file.getExtension() + "\"" );
long zipFileLength = doc_file.getSize();
res.addProperty(HttpHeaders.CONTENT_LENGTH, Long.toString(zipFileLength));
FileInputStream  input = (FileInputStream) doc_file.getContentStream();
OutputStream out = res.getPortletOutputStream();

byte[] buf = new byte[4096];
int bytesread = 0, bytesBuffered = 0;
System.out.println(input.toString().length());
while((bytesread = input.read(buf)) > -1) {
    out.write(buf, 0, bytesread);
    bytesBuffered += bytesread;
    if (bytesBuffered > 1024 * 1024) { //flush after 1MB
        bytesBuffered = 0;
        out.flush();
    }
}
input.close(); 
out.close(); 

此代码在Mozilla中运行,Mozilla显示的是文件总大小和下载数据的大小,但在Internet Explorer和Chrome中没有。

0 个答案:

没有答案