7 Replies Latest reply: Jul 14, 2013 1:37 PM by Neq RSS

    How to download BLOB generated by PL/SQL function?

    Neq

      Hi!

       

      I have a problem with combining fileDownloadActionListener and BLOB returned by PL/SQL function.

       

      Simply, I have a ADF table with some parameters for function and commandLink in each row. CommandLink calls some Java (getBlob method) to get BLOB (BlobDomain) from PL/SQL function. This part works perfectly.

      Next, fileDownloadActionListener should call some method like this:

       

          public void downloadImage(FacesContext facesContext, OutputStream outputStream)
          {
              BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();

       

              // get an ADF attributevalue from the ADF page definitions
              AttributeBinding attr = (AttributeBinding) bindings.getControlBinding("getBlob");
              if (attr == null)
              {
                  return;
              }

       

              // the value is a BlobDomain data type
              BlobDomain blob = (BlobDomain) attr.getInputValue();

       

              try
              {   // copy hte data from the BlobDomain to the output stream
                  IOUtils.copy(blob.getInputStream(), outputStream);
                  // cloase the blob to release the recources
                  blob.closeInputStream();
                  // flush the outout stream
                  outputStream.flush();
              }
              catch (IOException e)
              {
                  // handle errors
                  e.printStackTrace();
                  FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");
                  FacesContext.getCurrentInstance().addMessage(null, msg);
              }
          }

       

      and here is the problem: I get blank page. No errors, just nothing.

       

      I'm not sure what is wrong. Thanks for any help.

       

      I'm runing JDeveloper 11.1.1.6.0

        • 1. Re: How to download BLOB generated by PL/SQL function?
          Timo Hahn

          Have you checked that the blob domain you get from the attribute contains the right data?

          Copy the data from the blob into file and check if the data is correct.

           

          Timo

          • 2. Re: How to download BLOB generated by PL/SQL function?
            Neq

            Thanks for response!

             

            PS/SQL function works fine. Blob saved to file on DB side contains proper data.

             

            I'm not sure how to check blob content on Weblogic side, because I cannot run the application. After running a page I get:

             

            [Running application Promotions4 on Server Instance IntegratedWebLogicServer...]
            [08:05:09 AM] ----  Deployment started.  ----
            [08:05:09 AM] Target platform is  (Weblogic 10.3).
            [08:05:11 AM] Retrieving existing application information
            [08:05:11 AM] Running dependency analysis...
            [08:05:11 AM] Deploying 3 profiles...
            [08:05:12 AM] Wrote Web Application Module to C:\.....\JDeveloper\system11.1.1.6.38.62.29\o.j2ee\drs\Promotions4\ViewControllerWebApp.war
            [08:05:12 AM] Wrote Web Application Module to C:\.....\JDeveloper\system11.1.1.6.38.62.29\o.j2ee\drs\Promotions4\ModelWebApp.war
            [08:05:13 AM] Wrote Enterprise Application Module to C:\.....\JDeveloper\system11.1.1.6.38.62.29\o.j2ee\drs\Promotions4
            [08:05:14 AM] Deploying Application...
            [08:05:32 AM] Application Deployed Successfully.
            [08:05:32 AM] The following URL context root(s) were defined and can be used as a starting point to test your application:
            [08:05:32 AM] http://localhost:7101/Promotions4-Model-context-root
            [08:05:32 AM] http://localhost:7101/Promotions4-ViewController-context-root
            [08:05:32 AM] Elapsed time for deployment:  23 seconds
            [08:05:32 AM] ----  Deployment finished.  ----
            Run startup time: 23572 ms.
            [Application Promotions4 deployed to Server Instance IntegratedWebLogicServer]

             

            Target URL -- http://127.0.0.1:7101/Promotions4-ViewController-context-root/faces/main.jspx

             

            but this page is empty (literaly contains nothing), so I cannot run blob generation.

             

            btw. target url looks strange... faces/main.jspx?

            • 3. Re: How to download BLOB generated by PL/SQL function?
              Timo Hahn

              If your page does not run, how do you know that the problem is the blob?

              You first have to make sure the application runs before you can go into downloading a blob to the client.

              Which jdev version do you use?

              Normally the url doesn't contain the suffix '.jspx', it should end with 'main'.

               

              Timo

              • 4. Re: How to download BLOB generated by PL/SQL function?
                Neq

                If your page does not run, how do you know that the problem is the blob?

                You first have to make sure the application runs before you can go into downloading a blob to the client.

                It's not exactly problem with blob. PL/SQL function create proper blob, Java method that call thin function also works properly.

                 

                Problem occur when I try to use fileDownloadActionListener to tell browser that there is file to download.

                 

                in adfc-config.xml:

                <?xml version="1.0" encoding="windows-1250" ?>
                <adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
                  <managed-bean id="__7">
                    <managed-bean-name id="__6">DownloadBean</managed-bean-name>
                    <managed-bean-class id="__8">view.downloadArchive</managed-bean-class>
                    <managed-bean-scope id="__5">request</managed-bean-scope>
                  </managed-bean>
                </adfc-config>

                in main.jspx:

                                <af:commandLink actionListener="#{bindings.getPromotionsZipArchive.execute}"
                                                text="getPromotionsZipArchive"
                                                disabled="#{!bindings.getPromotionsZipArchive.enabled}"
                                                id="cl1">
                                  <af:fileDownloadActionListener contentType="application/zip"
                                                                 filename="archive.zip"
                                                                 method="#{DownloadBean.downloadArchive}"/>
                                  <f:facet name="context"/>
                                </af:commandLink>

                DownloadBean is a simple class with one method downloadArchive which source code I posted above.

                 

                In this place something unexpected is happening: application runs without any error, target URL is changed and page is blank.

                 

                Which jdev version do you use?

                JDeveloper Studio 11.1.1.6.0

                 

                ---

                Sorry for my english, I'm not very familliar with it. I hope you understand everything :)

                • 5. Re: How to download BLOB generated by PL/SQL function?
                  Timo Hahn

                  Sorry,

                  Target URL -- http://127.0.0.1:7101/Promotions4-ViewController-context-root/faces/main.jspx

                   

                  but this page is empty (literaly contains nothing), so I cannot run blob generation.

                  If this is your start page and it comes up empty, it mean that the app is not running correct. Nothing can be done until you can't see a page with our link on. Only after you click on the link the blob is send to the client.

                   

                  Again, as long as you done't see a page (or an empty page) we can't say that the problem is the fileDownloadListener. There are a couple of reasons for a blank page, but you have not given any information that we can use to determine the reason for this.

                   

                  Do you use security in your application?

                  Have you tried to call the url like

                  http://127.0.0.1:7101/Promotions4-ViewController-context-root/faces/main

                   

                  Timo

                  • 6. Re: How to download BLOB generated by PL/SQL function?
                    Neq

                    Do you use security in your application?

                    Not yet.

                     

                    Have you tried to call the url like

                    http://127.0.0.1:7101/Promotions4-ViewController-context-root/faces/main

                    Yes, now I get following error:

                    OracleJSP error: java.io.FileNotFoundException:

                    Set the init-param debug_mode to "true" to see the complete exception message.


                    It looks like problem should be here (underlined place). I really not sure what exactly should be there.

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

                        public void downloadArchive(FacesContext fctx, OutputStream os) {
                            BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
                            AttributeBinding attr = (AttributeBinding) bindings.getControlBinding("getPromotionsZipArchive");

                            if (attr == null) {
                                return;
                            }

                            BlobDomain blob = (BlobDomain) attr.getInputValue();

                            try {
                                IOUtils.copy(blob.getInputStream(), os);
                                blob.closeInputStream();
                                os.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");
                                FacesContext.getCurrentInstance().addMessage(null, msg);
                            }
                    }
                    }

                    getPromotionsZipArchive is a method from AppModule that looks like this:

                        public BlobDomain getPromotionsZipArchive(String p_symbol, Date p_data_od,
                                                                  Date p_data_do,
                                                                  Integer p_okres) {
                            BlobDomain blob;
                            CallableStatement cs;
                            try {
                                cs =
                    getDBTransaction().createCallableStatement("begin ? := promo_pkg.gen_archive(?,?,?,?); end;",
                                                                0);
                                cs.registerOutParameter(1, Types.BLOB);
                                cs.setString(2, p_symbol);
                                cs.setDate(3, new java.sql.Date(p_data_od.dateValue().getTime()));
                                cs.setDate(4, new java.sql.Date(p_data_do.dateValue().getTime()));
                                cs.setInt(5, p_okres);
                                cs.executeUpdate();
                                blob = new BlobDomain(cs.getBlob(1));
                                return blob;
                            } catch (SQLException e) {
                                throw new JboException(e);
                            }
                        }
                    }

                    • 7. Re: How to download BLOB generated by PL/SQL function?
                      Neq

                      Ok, I solved the problem. I create application again from scratch (but the same sources) and now downloading works, but there is another problem: file has 0 bytes. It should have about 300-400 kB.

                      It's not a content type problem as you mentioned here: https://forums.oracle.com/thread/2398710#10374851

                      I tried also this way: https://forums.oracle.com/thread/964843#3788219 but no sucess.

                      Even this not works: http://docs.oracle.com/cd/E16162_01/apirefs.1112/e17491/tagdoc/af_fileDownloadActionListener.html

                      Debugger also works in some strange way, it passes through all breakpoints without stopping... I don't know something again?

                       

                      I do not know, I give up...