由于我所有的其他消息都是JSON,因此我认为我会将我的android解决方案转换为使用JSON多部分消息从相机发送到WCF服务的图像。我认为我有发送工作,但不知道如何反序列化。我不base64编码的原因是我想android 2.1工作和base64编码不起作用(至少这是我已阅读,并且唯一的“黑客”,我发现只有小文件的工作)。使用JSON和MultipartEntity从Android发送图像到WCF
所以在Android的我尝试发送图像:
public void upload() throws Exception {
//Url of the server
String url = "http://192.168.0.10:8000/service/UploadImage";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity mpEntity = new MultipartEntity();
//Path of the file to be uploaded
String filepath = path;
File file = new File(filepath);
ContentBody cbFile = new FileBody(file, "image/jpeg");
//Add the data to the multipart entity
mpEntity.addPart("image", cbFile);
post.setEntity(mpEntity);
//Execute the post request
HttpResponse response1 = client.execute(post);
//Get the response from the server
HttpEntity resEntity = response1.getEntity();
String Response=EntityUtils.toString(resEntity);
Log.d("Response:", Response);
client.getConnectionManager().shutdown();
}
WCF的(因为它是当我将使用httpurlconnect和OutputStream从机器人)代码。这是工作,那么:d:
public string UploadImage(Stream image)
{
var buf = new byte[1024];
var path = Path.Combine(@"c:\tempdirectory\", "test.jpg");
int len = 0;
using (var fs = File.Create(path))
{
while ((len = image.Read(buf, 0, buf.Length)) > 0)
{
fs.Write(buf, 0, len);
}
}
return "hej";
}
接口的WCF [OperationContract的] [WebInvoke( 方法= “POST”, UriTemplate = “/ UploadImage”,ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string UploadImage(流图像);
如果它很重要,在运行WCF
static void Main(string[] args)
{
string baseAddress = "http://192.168.0.10:8000/Service";
ServiceHost host = new ServiceHost(typeof(ImageUploadService), new Uri(baseAddress));
WebHttpBinding binding = new WebHttpBinding();
binding.MaxReceivedMessageSize = 4194304;
host.AddServiceEndpoint(typeof(IImageUploadService),binding , "").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
Console.ReadKey(true);
}
所以,现在的问题,consoleapplication我怎么解析进来的JSON流?有没有更好的方法来做到这一点?
注:我尝试设置提琴手,但3小时后,甚至没有得到它甚至读取流量,我给了ut。
有没有一个很好的方法来真正调试这段代码?
忘了包括结果,如果我流转换为字节数组,并保存到文件:
--IZZI8NmDZ-Id7DWP5z0nuPPZspVAGglcfEY9
Content-Disposition: form-data; name="image"; filename="mypicture.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
ÿØÿá°Exif and other funny letters of cause :D ending with
--IZZI8NmDZ-Id7DWP5z0nuPPZspVAGglcfEY9--
有了一些新的代码,我可以设法让这个
--crdEqve1GThGGKugB3On0tGNy5h2u746
Content-Disposition: form-data; name="entity"
{"filename":"mypicture.jpg"}
--crdEqve1GThGGKugB3On0tGNy5h2u746
Content-Disposition: form-data; name="file"; filename="mypicture.jpg"
Content-Type: application/octet-stream
ÿØÿá´Exif and the whole image here ...
新的更新例程看起来像这样:
public void uploadFile() {
String filepath = path;
File file = new File(filepath);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://192.168.0.10:8000/service/UploadImage");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
//Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();
//Add some values
jsonObj.put("filename", file.getName());
//Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
}
catch (JSONException e) {
Log.v("App", e.getMessage());
}
catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}
FileBody fileBody = new FileBody(file);//, "application/octet-stream");
reqEntity.addPart("file", fileBody);
try {
postRequest.setEntity(reqEntity);
//Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);
//Check the status code, in this case "created"
if(((HttpResponse) httpResp).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.v("App","Created");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
仍然我需要一种方法来区分不同的部分流,所以我可以分割json消息部分(如果我需要这些),然后获取图像的bytearray作为独立的部分来存储。我想我可以跳过json并返回到我的原始JUST发送图像的bytearray,但是我也需要能够处理JSON消息。
感谢您的评论到目前为止。
它可能是一个字节流,我的知识是非常新的:D。但是,它每块读取1024个字节并将其输出到文件。我已经添加了该文件的一部分,其大小几乎为2MB,这是正确的。 – Todilo
肯定是一个字节流,而不是文本。请查看[this](http://msdn.microsoft.com/zh-cn/library/system.io.stream.read.aspx)有关使用Stream.read()的示例代码 – toadzky