Forum Stats

  • 3,816,120 Users
  • 2,259,147 Discussions
  • 7,893,392 Comments

Discussions

ByteArray

managed BEAN
managed BEAN Member Posts: 948 Silver Badge
edited Apr 3, 2018 10:56AM in Java Programming

Hello,

I have a byte array and would like to show it´s content in a web application using servlet.

Using SoapUI this is what i get into my application:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   <soapenv:Body>      <runReportInSessionResponse xmlns="http://xmlns.oracle.com/oxp/service/v2">         <runReportInSessionReturn>            <metaDataList xsi:nil="true"/>            <reportBytes>JVBERi0xLjYNCjUgMCBvYmoNCjw8DQovVHlwZSAvUGFnZQ0KL1BhcmVudCAzIDAgUg0KL1Jlc291cmNlcyA0IDAgUg0KL0NvbnRlbnRzIDYgMCBSDQovTWVkaWFCb3hbIDAgMCA2MTIuMCA3OTIuMCBdDQovQ3JvcEJveFsgMCAwIDYxMi4wIDc5Mi4wIF0NCi9Sb3RhdGUgMA0KPj4NCmVuZG9iag0KNiAwIG9iag0KPDwgL0xlbmd0aCA3ODcgL0ZpbHRlciAvRmxhdGVEZWNvZGUgPj4NCnN0cmVhbQ0KeJzVVstuU0EM3UfKP3gJm+nYY3tmlrQqSAgKbcMCIVSF5lECfZBGBb6XBb+B5ybcR9SSUtikSdP0zNhzjn0Sjwdvj6Nn1R8P82m/tzvo97D6D4FIXI4EMQQnSDA47/d2nmK/B8QwmPR7j07PhvPF64/jo/HV5XzxeDArweVhOZdvSs59yxkUOKFjII1AITmF+Rgsx5fqufPsGGF6beHOTl79llgPzy2L5bUQyvYDnMlp9Y7U267C0q+S9XuHVTrvOGZEaV7hK3hH4nPy7Eu4pHQvxJS8e2/lGD0wviOCNLmMTJyTYEBEUC8uhUweOQfNVpxzqHeBiClUFCsZBReJI4J4donZUqNGZ2dJamGngEiOgrK2UNvoWTO1EtbQbwYW2mCKTlETtbJFr7YvCLb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AwMDAwIG4NCjAwMDAwMDEwODggMDAwMDAgbg0KMDAwMDAwMTE3MCAwMDAwMCBuDQowMDAwMDAxMjM4IDAwMDAwIG4NCjAwMDAwMDAwMTAgMDAwMDAgbg0KMDAwMDAwMDE2OCAwMDAwMCBuDQowMDAwMDAxNDI5IDAwMDAwIG4NCjAwMDAwMDE1MzQgMDAwMDAgbg0KMDAwMDAwMTYwOCAwMDAwMCBuDQowMDAwMDAxNjgyIDAwMDAwIG4NCjAwMDAwMDE3NTkgMDAwMDAgbg0KMDAwMDAwMTgzNiAwMDAwMCBuDQowMDAwMDAxOTEzIDAwMDAwIG4NCjAwMDAwMDE5OTAgMDAwMDAgbg0KdHJhaWxlcg0KPDwNCi9TaXplIDE1DQovUm9vdCAxIDAgUg0KL0luZm8gMiAwIFINCi9JRCBbPDhhMTQyOTMwZmY4M2U2OGUyZTdjYTc5MTgzZWE3MzYzPjw4YTE0MjkzMGZmODNlNjhlMmU3Y2E3OTE4M2VhNzM2Mz5dDQo+Pg0Kc3RhcnR4cmVmDQoyMDY3DQolJUVPRg0K</reportBytes>            <reportContentType>application/pdf</reportContentType>            <reportFileID xsi:nil="true"/>            <reportLocale xsi:nil="true"/>         </runReportInSessionReturn>      </runReportInSessionResponse>   </soapenv:Body></soapenv:Envelope>

The servlet i use is:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String byteString = (request.getParameter("bytes"));byte[] bytes = byteString.getBytes();OutputStream os = response.getOutputStream();try {    byte[] buffer = bytes;    os.write(buffer, 0, buffer.length);} catch (Exception e) {    System.out.println(e);} finally {    if (os != null) {        os.close();    }}}

but it display nothing in browser.

pastedImage_9.png

If i pass the content Type (which is a string value) it shows it in browser, so code is correct (perhaps not completed for pdf).

pastedImage_8.png

Can you help me?

Thanks

Carlos

Answers

  • RuGI:.
    RuGI:. Member Posts: 67 Bronze Badge
    edited Mar 13, 2018 8:02PM

    The browser requires to know the mime-type to know if it can or not render the file.

    You can know the mime-type from the ByteArray using something like ApacheTika or jMimeMagic

    ---

  • managed BEAN
    managed BEAN Member Posts: 948 Silver Badge
    edited Mar 14, 2018 5:19AM

    Hello,

    Thanks for response.

    I think you are rigth, but in that case how does the browser knows the mimetype of following code of servlet:

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {path = "/home/weblogic/ccarvalho/OracleBIP/examples/javascript.pdf";try {    File outputFile = new File(path);    inputStream = new FileInputStream(outputFile);    BufferedInputStream in = new BufferedInputStream(inputStream);    int                 b;    byte[]              buffer = new byte[10240];    while ((b = in.read(buffer, 0, 10240)) != -1) {        os.write(buffer, 0, b);    }

    ?

    Cause that shows the pdf correct

    Regards,

    Carlos

  • RuGI:.
    RuGI:. Member Posts: 67 Bronze Badge
    edited Mar 14, 2018 10:55AM

    Perhaps in the case of the servlet the byteArray is not being correctly received or is incomplete.

    Place thecode (with FileInputStream) in the servlet and validate the result.

    ----

  • managed BEAN
    managed BEAN Member Posts: 948 Silver Badge
    edited Mar 15, 2018 5:49AM

    Hello,

    In the meantime i found out that the service (the postman printScreen of original post) was replying (implicitly) the result in Base64 format.

    So started to decode it in client in a java class to validate and was ok, but when i put that code in the servlet it gives null after the decoding part:

        public void doGet(HttpServletRequest request, HttpServletResponse response)        throws ServletException, IOException {        String byteString = (request.getParameter("bytes"));        System.out.println("byteString" + byteString);        byte[] bytes = Base64.getDecoder().decode(byteString);        System.out.println("bytes" + bytes);                //response.setContentType("application/pdf" + "; charset=utf8");                FileOutputStream fos = new FileOutputStream("/home/weblogic/ccarvalho/OracleBIP/examples/test.pdf");        fos.write(bytes);        fos.close();                OutputStream os = response.getOutputStream();

    and the log from console is:

    byteStringJVBERi0xLjYNCjUgMCBvYmoNCjw8DQovVHlwZSAvUGFnZQ0KL1BhcmVudCAzIDAgUg0KL1Jlc291cmNlcyA0IDAgUg0KL0NvbnRlbnRzIDYgMCBSDQovTWVkaWFCb3hbIDAgMCA2MTIuMCA3OTIuMCBdDQovQ3JvcEJveFsgMCAwIDYxMi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wSmNZsGQdvtM4HkupIJM8Fxy 00oeuBaLEm8uGptpLe8OEeN4ofIhQZHmM8WbIpKpwFRZiIrSTFEaq9OBx1IhKo5JRwbsuWO61ArjvNVkCsE0PM4tNlPHREZiM c92BWSYCKvL6mQoMiVVUgCb yhYKZj/pSFiB3L1uBAJ17RuY6hzPJ8KQSTd 5QKBVteZgXjOQpUqp1Hn 6UJjeRbKy0D9JkiukwHhe92xBphUVouX5zBSSo3i7LWTH8b dFLDjirklKdwjSvFoDTgFnkdcwRTtAulCULTkRec0mF8Bsngq1Q0KZW5kc3RyZWFtDQplbmRvYmoNCjggMCBvYmoNCjw8DQovVHlwZSAvUGFnZQ0KL1BhcmVudCAzIDAgUg0KL1Jlc291cmNlcyA0IDAgUg0KL0NvbnRlbnRzIDkgMCBSDQovTWVkaWFCb3hbIDAgMCA2MTIuMCA3OTIuMCBdDQovQ3JvcEJveFsgMCAwIDYxMi4wIDc5Mi4wIF0NCi9Sb3RhdGUgMA0KPj4NCmVuZG9iag0KOSAwIG9iag0KPDwgL0xlbmd0aCAxMTUxIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlID4 DQpzdHJlYW0NCnichVfbbttGEH034H YPqUF0uned5mXwk7cpqjhBLaANo9ram2z4cXgJYb vkPKMURrtoUAQeLOmfucHaJxDvDl6/r30xNcP rvT0/ON6cnv/wmIcDmjgSClIDBbytesnull<15-Mar-2018, 9:41:03,781 AM WET> <Error> <HTTP> <BEA-101020> <[[email protected][app:Booq module:Booq path:null spec-version:3.1 version:V0.0.133.0]] Servlet failed with an Exceptionjava.lang.NullPointerExceptionat java.io.FileOutputStream.write(FileOutputStream.java:313)at com.concept.booq.view.servlets.PdfServlet.doGet(PdfServlet.java:37)at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)Truncated. see log file for complete stacktrace> 

    So there is an issue in decoding the Base64format to byteArray.

    Solving that should do the trick.

    Thanks,

    Carlos

    P.S. - line 37 is line 13 of doGet method of servlet (fos.write(bytes))

  • managed BEAN
    managed BEAN Member Posts: 948 Silver Badge
    edited Apr 3, 2018 10:56AM

    Hello,

    Found that changing the library of Base64 to org.apache.commons.codec.binary.Base64 helped solving the issue, although i went to a different approach that does not need a servlet now.

    I convert it to image (Blob) and show it in web page.

    Done.

    Thanks everyone.

    Regards,

    Carlos

This discussion has been closed.