Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java向REST服务发送数据_Java_Web Services_Rest_Put - Fatal编程技术网

从java向REST服务发送数据

从java向REST服务发送数据,java,web-services,rest,put,Java,Web Services,Rest,Put,我已经用C实现了REST服务来上传图像: [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "add/{idAlbum}/{name}/image")] void Add(string idAlbum, string name, Stream image); 我已成功地将其用于C客户端: byte[] image = lireFichier(@"C:\Users\user\Pictures\asap2.jpeg"); We

我已经用C实现了REST服务来上传图像:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "add/{idAlbum}/{name}/image")]
void Add(string idAlbum, string name, Stream image);
我已成功地将其用于C客户端:

byte[] image = lireFichier(@"C:\Users\user\Pictures\asap2.jpeg");
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "image/jpeg");
var results = client.UploadData("http://localhost:1767/ImageService.svc/add/1/REST/image", "PUT", image);
因此,现在我想将其与java客户端android一起使用,如下所示:

HttpURLConnection conn = ( HttpURLConnection ) new URL( "http://localhost:1767/ImageService.svc/add/1/RESTjava/image" ).openConnection();  

conn.setRequestMethod("PUT");

conn.setDoOutput( true );
conn.connect(); 

OutputStream out = conn.getOutputStream();  

Bitmap img = ((BitmapDrawable)getResources().getDrawable(R.drawable.entourage)).getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
out.write(data);
我没有任何错误,但这不起作用。没有例外

 06-05 14:11:57.736: I/ASAP PICS(746): onPreExecute
 06-05 14:11:57.745: I/ASAP PICS(746): doInBackground
 06-05 14:11:57.816: I/System.out(746): 405
 06-05 14:44:26.245: D/dalvikvm(971): GC freed 238 objects / 332400 bytes in 34ms
 06-05 14:11:57.905: I/ASAP PICS(746): onPostExecute
您应该对OutputStream:out.flush;执行刷新;。这就是答案

I/System.out746:405

HTTP 405=不允许使用方法


我想您必须添加身份验证?

我找到了解决方案

首先,我将REST服务放在POST方法中:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "add/{idAlbum}/{name}")]
void Add(string idAlbum, string name, Stream image);
最后是java客户端:

HttpPost post = new HttpPost("http://localhost:1767/ImageService.svc/add/1/RESTjava");

Bitmap img = ((BitmapDrawable)getResources().getDrawable(R.drawable.entourage)).getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();

ByteArrayEntity bimg = new ByteArrayEntity(data);
post.setEntity(bimg);
new DefaultHttpClient().execute(post);

例外情况?返回代码?无例外。06-05 14:11:57.736:I/ASAP PICS746:onPreExecute 06-05 14:11:57.745:I/ASAP PICS746:doInBackground 06-05 14:11:57.816:I/System.out746:40506-05 14:11:57.905:I/ASAP PICS746:onPostExecute@user2455427加上out.close;不工作,但我在日志中还有一条消息:06-05 14:13:15.415:D/dalvikvm809:GC在44msI中释放了1128个对象/72032字节。我认为这是正常的,因为我在使用浏览器访问url时看到了这条消息。但是这是C客户机的工作,然后你必须检查C客户机的不同之处:我打赌其中涉及某种身份验证。