1

我正在尝试将一张图片发送到服务。在发送图像之前,我将图像转换为字符串,以便服务可以读取此字符串并写入新文件以进行进一步处理。此代码适用于 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 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)

我试图更改以下代码

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

请让我知道这里有什么问题。

4

0 回答 0