1

I'm trying to send one image to a service. Before sending image I'm converting image to String so that the service can read this String and write to a new file for further processing. This code is working fine with txt file but not with pdf/image Here is the code to call this service -

       File file = new File("C:\\Capacity planning.jpg");
    byte[] bFile = new byte[(int) file.length()];
    FileInputStream fileInputStream=null;
    fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();

        String s = new String(bFile);

    String ss =  
         "http://host:post/DemoService/services/capture/"+URLEncoder.encode(s, "UTF-
             8");
       HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(ss);
    HttpResponse response = client.execute(request);    

While executing this code I got the below error -

org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: Retrying request Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error

at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at  org.apache.http.impl.io.AbstractSessionOutputBuffer.flushBuffer
            (AbstractSessionOutputBuffer.java:131)
at org.apache.http.impl.io.AbstractSessionOutputBuffer.writeLine
           (AbstractSessionOutputBuffer.java:223)
at org.apache.http.impl.io.HttpRequestWriter.writeHeadLine
               (HttpRequestWriter.java:57)
at org.apache.http.impl.io.AbstractMessageWriter.write
            (AbstractMessageWriter.java:89)
at org.apache.http.impl.AbstractHttpClientConnection.sendRequestHeader
           (AbstractHttpClientConnection.java:250)
at org.apache.http.impl.conn.DefaultClientConnection.sendRequestHeader
           (DefaultClientConnection.java:266)
at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestHeader
           (AbstractClientConnAdapter.java:235)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest
              (HttpRequestExecutor.java:219)
at org.apache.http.protocol.HttpRequestExecutor.execute
                 (HttpRequestExecutor.java:123)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute
                 (DefaultRequestDirector.java:622)
at org.apache.http.impl.client.DefaultRequestDirector.execute
                 (DefaultRequestDirector.java:454)
at org.apache.http.impl.client.AbstractHttpClient.execute
                 (AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute
                 (AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.
                 execute(AbstractHttpClient.java:732)

I tried to change the following code

            String ss =  "http://host:port/DemoService/services/capture/
                           "+URLEncoder.encode(s, "UTF-8");

to

   String ss = "http://host:port/DemoService/
                services/capture/"+URLEncoder.encode(bFile.toString(), "UTF-8");

In this case, I'm able to call service but server side code is not able to create new file properly. Following is the service code -

      @GET
    @Path("/{param}")
   @Produces(MediaType.TEXT_PLAIN)
 public String capturePhoto(@PathParam("param") String msg) throws IOException {
    File file;
    FileOutputStream fop = null;
    file = new File("C:\\Code\\images1.jpg");
    fop = new FileOutputStream(file);
    byte[] contentInBytes = msg.getBytes();
    fop.write(contentInBytes);
    fop.flush();
    fop.close();
           }

Kindly let me know whats wrong here.

Pakira
  • 1,951
  • 3
  • 25
  • 54
  • Does the server side log an error in its log file? – Robin Green Oct 26 '13 at 23:18
  • @Robin, call is not even going to Server – Pakira Oct 27 '13 at 01:07
  • Obviously. The exception is being thrown by `sendRequestHeader().` – user207421 Oct 27 '13 at 01:30
  • @EJP, I checked that link that you specified but it does not talk about the resolution. Server code and client are both on the same machine in my case. – Pakira Oct 27 '13 at 01:56
  • @EJP You said, "In this case, I'm able to call service but server side code is not able to create new file properly. " I am talking about this case. – Robin Green Oct 27 '13 at 08:26
  • When you convert a not-text file (like pdf or jpeg) to a String, the non readable characters gets depreciated. So only readable characters are left. When you convert back them into a file, large chunk of data is lost and you get corrupted file or some sort of exceptions. I would suggest you not to convert anything into a String. If you really want to convert into something similar to String, convert it into byte array using getBytes(). your error lies in the line `String s = new String(bFile);` – maxx777 Apr 11 '14 at 20:56
  • @RobinGreen I did not say any such thing. I don't know what you're talking about. – user207421 May 17 '16 at 11:51

0 Answers0