Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Strange error when downloading blob

Paul (MITsa)Jul 25 2009 — edited Oct 5 2010
ADF 11g
Hello
I have a pdf file uploaded to the DB as a blob column.
I am now trying to download it.
I've tried to convert the Kuba [http://kuba.zilp.pl/?id=1] example from ADF 10 to 11.

I have page showing an af:table.
Clicking on the filename column runs the following :
    public BindingContainer getBindings() {
        FacesContext fc = FacesContext.getCurrentInstance();        
        return (BindingContainer)fc.getApplication().evaluateExpressionGet(fc,"#{bindings}", BindingContainer.class);
    }

    public String onDownLoad() {
         BindingContainer bindings = getBindings();
         DCIteratorBinding itr = (DCIteratorBinding) bindings.get ("SttAttachedFilesView1Iterator");
         Row r = itr.getCurrentRow();
         BlobDomain file = (BlobDomain)r.getAttribute("FileContent");
         String fileName = (String)r.getAttribute("FileName");
            
         FileOperations.downloadFile( fileName,  file);
           
         return null;
     }
The actual download code is
public class FileOperations {
    public FileOperations() {
    }
    public static String getMimeType(String fileName){
    
        String mime = null;
           
        String ext = fileName.toLowerCase();
        
             if(ext.endsWith(".pdf")) { mime = "application/pdf"; }
        else if(ext.endsWith(".doc")) { mime = "application/msword"; }
        else if(ext.endsWith(".ppt")) { mime = "application/vnd.ms-powerpoint"; }
        else if(ext.endsWith(".rar")) { mime = "application/octet-stream"; }
        else if(ext.endsWith(".zip")) { mime = "application/zip"; }
           
     return mime;
    }    
    
    public static synchronized void downloadFile( String fileName, BlobDomain blobDomain )
    {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext extContext = facesContext.getExternalContext();

        Long length = blobDomain.getLength();       
        String fileType = getMimeType(fileName);
        
        HttpServletResponse response = (HttpServletResponse) extContext.getResponse();
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        response.setContentLength((int) length.intValue());
        response.setContentType( fileType );
        try {
            InputStream in = blobDomain.getBinaryStream();
            OutputStream out = response.getOutputStream();

            byte[] buf = new byte[1024];
            int count;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }

            in.close();
            out.flush();
            out.close();
            facesContext.responseComplete();
            
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }        
}
Whats happening is the first time I click on the filename the file is downloaded from the DB correctly.
If I click a second time I get the following errors :
<25 juil. 2009 12.54. h CEST> <Error> <HTTP> <BEA-101083> <Connection failure.
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '156751' bytes.
	at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:425)
	at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1451)
	at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1494)
	at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1437)
	at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
	Truncated. see log file for complete stacktrace
>
<25 juil. 2009 12.54. h CEST> <Error> <HTTP> <BEA-101104> <Servlet execution in servlet context "ServletContext@18785780[app:UpLoadDownload module:UpLoadDownload-ViewController-context-root path:/UpLoadDownload-ViewController-context-root spec-version:2.5]" failed, java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '156751' bytes..
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '156751' bytes.
	at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:425)
	at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1451)
	at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1494)
	at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1437)
	at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
	Truncated. see log file for complete stacktrace
>
It as if the inputStream is empty the second time round even though the lenght is correct.
If I close the application and restart I can download the file again.
I've added an execute button to re-execute the query after the first download but the problem persists.

Anybody got any ideas ?

Regards
Paul
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2010
Added on Jul 25 2009
7 comments
6,530 views