上传文件:使用 Java Comsuming WCF Web 服务
Upload File: Comsuming WCF Web Service Using Java
我有一个 WCF Web 服务 (.NET C#)。我想通过 java 客户端应用程序使用该服务。
我使用以下代码成功连接到 Web 服务。但是现在我遇到了 uploadFile 方法的问题。
当我将字符串传递给请求实体时,它会调用 Web 服务方法,但是当我 passed/set FileInputStream RequestEntity 时,它会抛出异常...
Connection reset by peer: socket write error
我的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#代码
我服务
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "upload")]
bool upload(Stream relativePath);
我得到了答案。这是分配有效内容类型的问题。我正在设置内容类型
header.put("Content-type", "application/x-www-form-urlencoded");
使用以下内容类型后,我能够成功上传文件
httpConn.setRequestProperty("Content-Type","multipart/form-data");
我有一个 WCF Web 服务 (.NET C#)。我想通过 java 客户端应用程序使用该服务。
我使用以下代码成功连接到 Web 服务。但是现在我遇到了 uploadFile 方法的问题。
当我将字符串传递给请求实体时,它会调用 Web 服务方法,但是当我 passed/set FileInputStream RequestEntity 时,它会抛出异常...
Connection reset by peer: socket write error
我的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#代码
我服务
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "upload")]
bool upload(Stream relativePath);
我得到了答案。这是分配有效内容类型的问题。我正在设置内容类型
header.put("Content-type", "application/x-www-form-urlencoded");
使用以下内容类型后,我能够成功上传文件
httpConn.setRequestProperty("Content-Type","multipart/form-data");