473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Sending Image bytes through Java webservice

Hello All,

Scenario: Sending an image through webservice as byte array to an Java webservice.

The Problem1: The webservice method image property expects (data type) SByte rather than Byte array. Thus i'm converting a byte array to sbyte array and sending through web service. The converted SByte contains negative numbers wihch are resulting in an error "java.lang.ArrayIndexOutOfBoundsException: -106"

Byte[] => SByte[]...error "java.lang.ArrayIndexOutOfBoundsException: -106"

The Problem2: Also if i before converting Byte array to SByte array Encode the the Byte array and then later convert to Sbyte then the java web service method save the file on the server but it isn't recognised as an Image type file.

Byte[] => Encode base64 => SByte...Saved file isn't an Image(valid/original) file type.

If any one have dealth with similar set of situtation before, kindly post your answer.

-106
java.lang.ArrayIndexOutOfBoundsException: -106
at org.apache.commons.codec.binary.Base64.isBase64(Ba se64.java:137)
at org.apache.commons.codec.binary.Base64.discardNonB ase64(Base64.java:478)
at org.apache.commons.codec.binary.Base64.decodeBase6 4(Base64.java:374)
at com.recare.ws.scanning.ScannedPageWTO.initDTO(Unkn own Source)
at com.recare.ws.scanning.ScannedDocWTO.initDTO(Unkno wn Source)
at com.recare.ws.scanning.ScanDocWebService.saveNewDo cument(Unknown Source)
at com.recare.ws.scanning.ScanDocWebServiceImpl.saveN ewDocument(Unknown Source)
at com.recare.ws.scanning.ScanDocWebServiceMessageRec eiverInOut.jibxReceiver0(Unknown Source)
at com.recare.ws.scanning.ScanDocWebServiceMessageRec eiverInOut.invokeBusinessLogic(Unknown Source)
at org.apache.axis2.receivers.AbstractInOutMessageRec eiver.invokeBusinessLogic(AbstractInOutMessageRece iver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver .receive(AbstractMessageReceiver.java:100)
at org.apache.axis2.engine.AxisEngine.receive(AxisEng ine.java:176)
at org.apache.axis2.transport.http.HTTPTransportUtils .processHTTPPostRequest(HTTPTransportUtils.java:27 5)
at org.apache.axis2.transport.http.AxisServlet.doPost (AxisServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

Thanks in advance
Ksheera Sagar
Jul 4 '09 #1
2 9743
Ahh God !! Thank u... i figured it out at last and also thanks to my Project Manager. I re-Tried the a code snipet sent by him. The funda being it is absolutely possible to send an image as bytes through a webservice but if encoded correctly.
In my case i had to send in image as sbytes rather than bytes.
Below is the code snipet which Encodes byte array to Base64 and returns/converts the byte array to sbyte array.

Hope it works for u as well.

Expand|Select|Wrap|Line Numbers
  1.  /// <summary>
  2.         /// Encodes a byte array to Base64 and returns an sbyte array
  3.         /// </summary>
  4.         /// <param name="data">byte array</param>
  5.         /// <returns>sbyte array</returns>
  6.         public sbyte[] ToBase64SbyteFrombyte(byte[] data)
  7.         {
  8.             int length = data == null ? 0 : data.Length;
  9.             //if (length == 0)
  10.             //    return String.Empty;
  11.  
  12.             int padding = length % 3;
  13.             if (padding > 0)
  14.                 padding = 3 - padding;
  15.             int blocks = (length - 1) / 3 + 1;
  16.  
  17.             sbyte[] s = new sbyte[blocks * 4];
  18.  
  19.             for (int i = 0; i < blocks; i++)
  20.             {
  21.                 bool finalBlock = i == blocks - 1;
  22.                 bool pad2 = false;
  23.                 bool pad1 = false;
  24.                 if (finalBlock)
  25.                 {
  26.                     pad2 = padding == 2;
  27.                     pad1 = padding > 0;
  28.                 }
  29.  
  30.                 int index = i * 3;
  31.                 byte b1 = data[index];
  32.                 byte b2 = pad2 ? (byte)0 : data[index + 1];
  33.                 byte b3 = pad1 ? (byte)0 : data[index + 2];
  34.  
  35.                 byte temp1 = (byte)((b1 & 0xFC) >> 2);
  36.  
  37.                 byte temp = (byte)((b1 & 0x03) << 4);
  38.                 byte temp2 = (byte)((b2 & 0xF0) >> 4);
  39.                 temp2 += temp;
  40.  
  41.                 temp = (byte)((b2 & 0x0F) << 2);
  42.                 byte temp3 = (byte)((b3 & 0xC0) >> 6);
  43.                 temp3 += temp;
  44.  
  45.                 byte temp4 = (byte)(b3 & 0x3F);
  46.  
  47.                 index = i * 4;
  48.                 s[index] = (sbyte)SixBitToChar(temp1);
  49.                 s[index + 1] = (sbyte)SixBitToChar(temp2);
  50.                 s[index + 2] = (sbyte)(pad2 ? '=' : SixBitToChar(temp3));
  51.                 s[index + 3] = (sbyte)(pad1 ? '=' : SixBitToChar(temp4));
  52.             }
  53.  
  54.             return s;
  55.         }
  56.  
  57.         static private char SixBitToChar(byte b)
  58.         {
  59.             char c;
  60.             if (b < 26)
  61.             {
  62.                 c = (char)((int)b + (int)'A');
  63.             }
  64.             else if (b < 52)
  65.             {
  66.                 c = (char)((int)b - 26 + (int)'a');
  67.             }
  68.             else if (b < 62)
  69.             {
  70.                 c = (char)((int)b - 52 + (int)'0');
  71.             }
  72.             else if (b == 62)
  73.             {
  74.                 c = s_CharPlusSign;
  75.             }
  76.             else
  77.             {
  78.                 c = s_CharSlash;
  79.             }
  80.             return c;
  81.         }
  82.  
  83.         static private char s_CharPlusSign = '+';
  84.  
  85.         /// <summary>
  86.         /// Gets or sets the plus sign character.
  87.         /// Default is '+'.
  88.         /// </summary>
  89.         static public char CharPlusSign
  90.         {
  91.             get
  92.             {
  93.                 return s_CharPlusSign;
  94.             }
  95.             set
  96.             {
  97.                 s_CharPlusSign = value;
  98.             }
  99.         }
  100.  
  101.         static private char s_CharSlash = '/';
  102.  
  103.         /// <summary>
  104.         /// Gets or sets the slash character.
  105.         /// Default is '/'.
  106.         /// </summary>
  107.         static public char CharSlash
  108.         {
  109.             get
  110.             {
  111.                 return s_CharSlash;
  112.             }
  113.             set
  114.             {
  115.                 s_CharSlash = value;
  116.             }
  117.         }
Jul 9 '09 #2
Simply ad 128 if SByte Value is negative

Byte lByte
If (sByteValue < 0)
lByte = sByteValue+128
Oct 2 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kumar T via .NET 247 | last post by:
Could you please explain me the steps for consuming JavaWebservice through .NET client. I know how to connect through.NET webservice and I followed the same steps for this, but itdid not work out. ...
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
4
by: luckyabhishek | last post by:
Hi I am using a java webservice in a .NET application. The xml type of a field in this webservice is xsd:datetime. When i call the webservice from the application i get a deserialization error on...
2
by: Pablo | last post by:
Hi at all! How can i send a DATA (not a DataTime) Type to a Java WebService? In .NET we have only a DataTime that is incompatible with the Data (calendar) Type of Java! How can i resolve this...
0
by: Andrej | last post by:
hi, i have a java webservice which i would like to invoke from .net Application. i am able to generate the proxy class, but if i try to invoke the webservice i got an exception of type...
2
by: zmbharmal | last post by:
I have a .NET client My vendor has a Axis/Java Webservice I am able to pass the parameters to their webmethod. They can read it. When they return a Response, it is coming as NULL. The webservice...
0
jeffbroodwar
by: jeffbroodwar | last post by:
hi, i need help about consuming webservice created in java with vb6. i've created the webservice in netbeans with sun java server bundle and used MS Soap toolkit 3.0 on the client side. i've...
2
jeffbroodwar
by: jeffbroodwar | last post by:
hi, i need help about consuming webservice created in java with vb6. i've created the webservice in netbeans with sun java server bundle and used MS Soap toolkit 3.0 on the client side. i've...
3
by: zion | last post by:
Hello, How can I return image link with webservice that I could see it in web page? The image is on my hard disk and <img src="c:\pictures\test.jpg" /does not work. If I use <img src=http://My...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.