ADF - Re - opening download distributes not appropriate content.

I use JDeveloper 11.1.1.4 and have a problem with downloading files. The first time that a user clicks on the link command, the file opens correctly. If the user closes the file open in the application, and clicks the same link to re - open the file (in this case a PDF) they Gets an error from Adobe indicating the uploaded file is not a supported or because file type that the file has been damaged. It seems that he reads every file. I use a similar code to other posts I've found on this discussion forum. I'm adding my java code below and the Weblogic Server log messages and would appreciate all good advice for this problem.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.jbo.Row;
import oracle.jbo.ViewObject;
import oracle.jbo.domain.BlobDomain;

public class Download {
    public Download() {
        super();
    }

    public void downloadFile(FacesContext facesContext,
                             OutputStream outputStream) throws IOException {
        String mime = null;
        ExternalContext extContext = facesContext.getExternalContext();

        DCBindingContainer dc =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        System.out.println("Before getting iterator");
        DCIteratorBinding iter =
            dc.findIteratorBinding("hot_biz_docs_view1Iterator");
        ViewObject vo = iter.getViewObject();
        Row r = vo.getCurrentRow();
        BlobDomain bDomain = (BlobDomain)r.getAttribute("Document");
        String file = r.getAttribute("FileName").toString();

        String ext = file.toLowerCase();
        System.out.println("In MimeTypes:" + ext);

        if (ext.endsWith(".pdf")) {
            mime = "application/PDF";
        } else if (ext.endsWith(".doc")) {
            mime = "application/msword";
        } else if (ext.endsWith(".docx")) {
            mime =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if (ext.endsWith(".xls")) {
            mime = "application/vnd.ms-excel";
        } else if (ext.endsWith(".xls")) {
            mime =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        } else if (ext.endsWith(".txt")) {
            mime = "text/plain";
        } 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";
        } else if (ext.endsWith(".jpg")) {
            mime = "image/jpeg";
        }
        Long length = bDomain.getLength();

        HttpServletResponse response =
            (HttpServletResponse)extContext.getResponse();
        response.setHeader("Content-Disposition",
                           "attachment;filename=\"" + file + "\"");
        response.setContentLength((int)length.intValue());
        System.out.println(mime);
        response.setContentType(mime);
        InputStream in = null;
        try {
            in = bDomain.getBinaryStream();
            outputStream = response.getOutputStream();

            byte[] buf = new byte[1024];
            int count;
            while ((count = in.read(buf)) >= 0) {
                outputStream.write(buf, 0, count);
                if (count <= 0) {
                    System.out.println("End if while loop");
                    break;
                }
            }

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        in.close();
        outputStream.flush();
        outputStream.close();
        response.flushBuffer();
        facesContext.responseComplete();
        System.out.println("closed and responseComplete");
    }
< 22 August 2011 4:11:11 PM CDT > < error > < HTTP > < BEA-101083 > < connection failure.
java.net.ProtocolException: did not said Content-Length, wrote: '0' bytes instead of a stated: '3544' bytes.
at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:446)
at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1432)
at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1511)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1462)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
Truncated. check the log file full stacktrace
>
< 22 August 2011 4:11:11 PM CDT > < HTTP > < BEA-101104 > < error > < execution of the Servlet in the servlet context "[ServletContext@33250642[app:LIB_Test module: root LIB_Test-LibTestVC-context-path: / LIB_Test-LibTestVC-context-root spec-version: 2.5] ' failed, java.net.ProtocolException: did not said Content-Length, wrote: '0' bytes instead of a said: '3544' bytes...
java.net.ProtocolException: did not said Content-Length, wrote: '0' bytes instead of a stated: '3544' bytes.
at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:446)
at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1432)
at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1511)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1462)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
Truncated. check the log file full stacktrace
>

You must close the stream blob, otherwise that you can't read.
Use the method blob.closeInputStream () on your blob.

Timo

Tags: Java

Similar Questions

Maybe you are looking for