java.net.SocketException:软件导致连接中止:套接字写入错误

时间:2013-10-26 21:15:51

标签: java web-services sockets rest get

我正在尝试将一张图片发送到服务。在发送图像之前,我将图像转换为字符串,以便服务可以读取此字符串并写入新文件以进行进一步处理。这段代码可以正常使用txt文件但不能使用pdf / image以下是调用此服务的代码 -

       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);    

执行此代码时,我收到以下错误 -

org.apache.http.impl.client.DefaultRequestDirector tryExecute 信息:重试请求 线程“main”中的异常java.net.SocketException:软件导致连接中止:套接字写入错误

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)

我尝试更改以下代码

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

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

在这种情况下,我可以调用服务,但服务器端代码无法正确创建新文件。以下是服务代码 -

      @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();
           }

请告诉我这里错了什么。

0 个答案:

没有答案