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.