上传文件:使用Java

时间:2015-05-08 14:52:34

标签: java web-services wcf upload

我有一个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#CODE

IService

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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