【问题标题】:Upload File: Comsuming WCF Web Service Using Java上传文件:使用 Java 使用 WCF Web 服务
【发布时间】:2015-05-08 14:52:34
【问题描述】:

我有一个 WCF Web 服务 (.NET C#)。我想通过 java 客户端应用程序使用该服务。

我使用以下代码成功连接到 Web 服务。但是现在我遇到了 uploadFile 方法的问题。

当我将字符串传递给请求实体时,它会调用 Web 服务方法,但当我传递/设置 FileInputStream RequestEntity 时,它会抛出异常...

对等连接重置:套接字写入错误

我的 Java 客户端代码如下......

  • 请忽略:此处未添加日志记录...

        public void consumeService(){
            String sXML = null;
            String sURI = URI + "/upload";
            sXML = item;
            HashMap<String, String> header = new HashMap();
            header.put("Content-type", "application/x-www-form-urlencoded");
    
            File file = new File("C:\\Users\\admin\\Desktop\\P1130503.JPG");
            try {
    
               RequestEntity requestEntity= new InputStreamRequestEntity(
                        new FileInputStream(sFile), InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                headers.addHeader(key, value);
                }
            } catch (Exception ex) {
                Logger.getLogger(C3Service.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
            HttpMethodBase httpPostRequest = new PostMethod(url + buildParams());
            HttpClient client = new HttpClient();
            try {
                // add headers
                Iterator it = headers.entrySet().iterator();
                while (it.hasNext()) {
                Entry header = (Entry) it.next();
                httpPostRequest.addRequestHeader((String) header.getKey(), (String) header.getValue());
    
                }
               ((PostMethod) httpPostRequest).setRequestEntity(requestEntity);
                try {
                respCode = client.executeMethod(httpPostRequest);
                System.out.println("Response Code  "+respCode);
                response = httpPostRequest.getResponseBodyAsString();
                this.responsePhrase = httpPostRequest.getStatusText();
                System.out.println("Response  "+response);
                System.out.println("Response Phase  "+responsePhrase);
    
              }catch(Exception ex){
                    System.out.println("ErrorS "+ex.toString());
                } finally {
                  //  resp.close();
                httpPostRequest.releaseConnection();
                }
            }catch(Exception ex){
              System.out.println("ErrorD "+ex.toString());
            }
            finally {
                //client.c
            }
      }
    

注意:当我传递字符串并设置 StringRequestEntity 然后它工作文件。

 new StringRequestEntity(statusAsXml, "text/plain", Constants.DEFAULT_ENCODING)

C# 代码

IService

        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "upload")]
        bool upload(Stream relativePath);

【问题讨论】:

    标签: java web-services wcf upload


    【解决方案1】:

    我得到了答案。这是分配有效内容类型的问题。我正在设置内容类型

     header.put("Content-type", "application/x-www-form-urlencoded");
    

    使用以下内容类型后,我可以成功上传文件

    httpConn.setRequestProperty("Content-Type","multipart/form-data");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多