0

enter image description here

attached is an image file which i convert to byte array and writes back the byte array to a image file . it works fine . the value of the converted byte array is [B@5bc79255.

same file is attached ina n incident in HP service manager. and am using java wo connect trhough web service and download the attachment file as byte array and write to a image file . in this situation i face problem. this byte array which i retrieve from HPSM through the webservice function provided by them is [B@6e0e048a

as you can see values are also different. but surprisingly when i write [B@6e0e048a in to a image file , the size of the file is sero and nothing is there inside the file !!! where as if i convert the same file as byte array and writes back in to some image file its reproduced correctly!!

Mr. Roshan
  • 1,777
  • 13
  • 33
  • [B@6e0e048a and [B@5bc79255 are the addresser where your byte arrays are stored. Not the values of the byte arrays themselves. – Oliver Norin Jun 26 '18 at 07:38
  • if i had to go by this explanation , then the conclusion is the , the web service function (here get_value) is not returning the value of the file attached as a byte array but some adddresser or something . as i have already said am using java rmi to connect to the web service to retrive all the incident related details (including attachment) . this sounds odd... i mean what can i do with some address... !! how can i get the content of the file ,? – seetharaman Jun 26 '18 at 07:41
  • I'm guessing that you downloaded an image to a byte array and then tried to print that byte array using `System.out.println`. Is that correct? – Oliver Norin Jun 26 '18 at 07:43
  • just to be clear code 1 : ============= File fi = new File("C:\\Users\\seethark\\Pictures\\soap-prob-1.png"); byte[] fileContent = Files.readAllBytes(fi.toPath()); Path path2 = Paths.get("C:\\Users\\seethark\\Pictures\\destination_file2.png"); Files.write(path2,fileContent); System.out.println("coverted file content " + fileContent); – seetharaman Jun 26 '18 at 07:50
  • code 2 : byte [] image_byte = inc_repsonse_for_attachment.getModel().getInstance().getAttachments()[0].get_value(); Path path = Paths.get("C:\\Users\\seethark\\Pictures\\destinationfile_1.png"); Files.write(path,image_byte.toString().substring(3, 9).getBytes()); System.out.println("incident file content " + image_byte.toString().substring(3, 9) ); – seetharaman Jun 26 '18 at 07:51
  • code 1 tries to convert an i amage(attached) to byte array and writes the same to an image file code 2 tries to download the same image which is attached in some incident in HP service manager 9 , using some webservice function (get_value()). first byte array when wrritten to an image gives perfect recreation of image. second gives a zero byte file as result. so what get_value() actually doing ?? p.s i printed the two byte array to give more clarity . – seetharaman Jun 26 '18 at 07:51
  • you are right. i first try to write the byte array then trying to print the byte array value . and to not to confuse image_byte.toString().substring(3, 9).getBytes()) i chose this because when i gave simply "image_byte" it reprocuded a zero byte file and when i extracted the byte array part alone . (ignoring @, B , ]) and tried to give the reamaning as sub string , then also no use !!!!! – seetharaman Jun 26 '18 at 07:55
  • The reason you get different results when trying to print two arrays that should be identical is because `System.out.println` cannot actually print arrays. Instead, the method will print the memory address that points to where the arrays are stored in your RAM. Since they are two different arrays, they are stored at different places and have different memory addresses. – Oliver Norin Jun 26 '18 at 07:57
  • ok fine perfect !! but what i need is not two identical printed stuffs. two identical output files whose source is actually same!!! so any idea ... ? am sure that get_value returns a byte array... but how reproduce the file ... for your info an xml req sent via soap ui perfectly downloads the file! – seetharaman Jun 26 '18 at 08:01
  • If you need to view the content of an array, you can use a for loop to iterate through the array and print every element inside it instead. You can keep debugging from there. The reason you cannot produce an image file from your array is impossible to determine from the information you have provided. – Oliver Norin Jun 26 '18 at 08:04
  • My guess is however, that your array is actually empty. That would explain why you get a zero byte file. – Oliver Norin Jun 26 '18 at 08:07
  • get_value method returns a byte array , which needs to be written in a dat file. for this you suggestion is loop through the byte array using for loop and extract the content also u saying this byte array denotes a memory location which is different from my memory location in my system. so in that case how can i reproduce the file even by looping ?? – seetharaman Jun 26 '18 at 08:09
  • I do not suggest you loop through the array in order to extract each element and write them one by one to a file. I suggested that you extract each element and print them to the console in order to actually see what your array contains. – Oliver Norin Jun 26 '18 at 08:12
  • Also, the byte array does not denote a memory location. But if you try to print your array by using `System.out.println(fileContent);` You will get the memory address of the array and not the content of the array. Read this: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – Oliver Norin Jun 26 '18 at 08:14
  • image_byte[1] --> this itself giving index out of range exception !!! this image_byte holds the return value of the get_value function. i think , looping through this does not make any sense!!! also image_byte.length returns zero!!! – seetharaman Jun 26 '18 at 08:32
  • Then you have identified the problem. You are not receiving any data from the server. – Oliver Norin Jun 26 '18 at 09:11

1 Answers1

0

if you are using servlet to retrieve the bytes and if the image is base64 image, use :

while ((line = request.getReader().readLine()) != null)
                stringBuffer.append(line);

String base64Image = imageData.toString().split(",")[1];
            byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
            ByteArrayInputStream finalStream = new ByteArrayInputStream(stringBuffer);

and write finalStream to file.

  • am using java RMI. not servlets . there is a method called get_value in the given web service which will return a byte array. this byte array needs to be written in a file , thats all what i want. when i write the out put of the get value (which is a byte array) in to a file am getting a zero byte file!!!! – seetharaman Jun 26 '18 at 08:17
  • get value method will get the attachment inside the incident in the ticketing tool (HPServiceManager). – seetharaman Jun 26 '18 at 08:18