由于我的所有其他消息都是JSON,我以为我会将我的android解决方案转换为使用JSON多部分消息将相机拍摄的图像发送到WCF服务。我想我有发送工作,但不知道如何反序列化。我没有base64编码的原因是我希望android 2.1工作并且base64编码不起作用(至少这是我读过的,而且我发现的唯一“hack”只适用于小文件)。
所以在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(就像我使用来自android的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(Stream image);
如果重要,运行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);
}
现在问题是,我该如何解析崩溃的JSON流?有没有更好的方法呢?
注意:我尝试设置小提琴手,但是在3小时后甚至没有看到流量,我就给了它。
有没有一种实际调试此代码的好方法?
如果我将流转换为字节数组并将其保存到file:
,则忘记包含结果--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消息。
感谢您的评论到目前为止。
答案 0 :(得分:1)
我的第一个想法是它不是JSON流。它可能是一个字节流。此外,如果您的图像大于1024字节,您将无限读取和写入前1024个字节。您应该有一个偏移量变量,用于跟踪您已阅读的内容并在此之后开始阅读。