469,623 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

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 9392
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

Post your reply

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

Similar topics

1 post views Thread by Kumar T via .NET 247 | last post: by
7 posts views Thread by Christian Wilhelm | last post: by
reply views Thread by Andrej | last post: by
3 posts views Thread by zion | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.