如何将多个映像从android发送到WCF Rest服务作为流写入网络驱动器?

经过大量的谷歌搜索和搜索,我设法使用从android到我的WCF服务的multiparsers发送图像,但理想情况下,我想一次发送几个图像,而不是一遍又一遍地调用该方法,因为它需要更长的时间,并添加更多的开销。

这是我目前的代码

Android(取自这里的代码):

public static String postFile(Bitmap bitmap, String urlString) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlString); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); ByteArrayOutputStream bao = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 30, bao); byte[] data = bao.toByteArray(); //filename String fileName = String.format("File_%d.png",new Date().getTime()); ByteArrayBody bab = new ByteArrayBody(data, fileName); builder.addPart("image", bab); final HttpEntity yourEntity = builder.build(); class ProgressiveEntity implements HttpEntity { @Override public void consumeContent() throws IOException { yourEntity.consumeContent(); } @Override public InputStream getContent() throws IOException, IllegalStateException { return yourEntity.getContent(); } @Override public Header getContentEncoding() { return yourEntity.getContentEncoding(); } @Override public long getContentLength() { return yourEntity.getContentLength(); } @Override public Header getContentType() { return yourEntity.getContentType(); } @Override public boolean isChunked() { return yourEntity.isChunked(); } @Override public boolean isRepeatable() { return yourEntity.isRepeatable(); } @Override public boolean isStreaming() { return yourEntity.isStreaming(); } // CONSIDER put a _real_ delegator into here! @Override public void writeTo(OutputStream outstream) throws IOException { class ProxyOutputStream extends FilterOutputStream { /** * @author Stephen Colebourne */ public ProxyOutputStream(OutputStream proxy) { super(proxy); } public void write(int idx) throws IOException { out.write(idx); } public void write(byte[] bts) throws IOException { out.write(bts); } public void write(byte[] bts, int st, int end) throws IOException { out.write(bts, st, end); } public void flush() throws IOException { out.flush(); } public void close() throws IOException { out.close(); } } // CONSIDER import this class (and risk more Jar File Hell) class ProgressiveOutputStream extends ProxyOutputStream { public ProgressiveOutputStream(OutputStream proxy) { super(proxy); } public void write(byte[] bts, int st, int end) throws IOException { // FIXME Put your progress bar stuff here! out.write(bts, st, end); } } yourEntity.writeTo(new ProgressiveOutputStream(outstream)); } }; ProgressiveEntity myEntity = new ProgressiveEntity(); post.setEntity(myEntity); HttpResponse response = client.execute(post); return getContent(response); } public static String getContent(HttpResponse response) throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String body = ""; String content = ""; while ((body = rd.readLine()) != null) { content += body + "\n"; } return content.trim(); } 

C#WCF服务方法

 [WebInvoke(UriTemplate = "UploadPicture/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] public String UploadPicture(string filename, Stream fileStream) { WriteLog("Uploading picture..."); try { MultipartParser parser = new MultipartParser(fileStream); if (parser.Success) { string fileName = parser.Filename; string contentType = parser.ContentType; byte[] fileContent = parser.FileContents; FileStream fileToupload = new FileStream("\\\\OHS-SUN\\Tracker\\robbie\\" + filename, FileMode.Create); fileToupload.Write(fileContent, 0, fileContent.Length); fileToupload.Close(); fileToupload.Dispose(); fileStream.Close(); return "Success !!!"; } else { return "Exception!!!"; } } catch (Exception ex) { WriteLog("Uploading picture exception: " + ex.Message); } return "Picture uploaded!"; } 

我想从发送一个图像到发送几个图像,每个图像具有2个文本属性; 文件名以及与之关联的项目编号。 从本质上讲,两者都是我所需要的。 目前,我只是尝试将另一个addPart放到android位上,但后来我不知道如何添加元数据,我不知道如何根据名称解析它。 我使用任何第三方库都没关系,我现在在C#上使用的那个已经是一个了。

非常感谢!

我只是将它放在一个异步类中,而不是一次发送多个图像,并且每次最多同时发送它们,直到所有图像都在该特定会话中发送。 似乎工作正常,实现是一样的,所以我不必改变任何,这是好的。 如果有人希望我发布我做过的代码,我会很高兴。 这只是小片段,但在上面的代码中添加了。

好吧,我添加的主要部分是:

 public static class FileUploader extends AsyncTask implements Future { @Override protected void onPreExecute() { filesUploading ++; } @Override protected String doInBackground(UploadFile... uploadFile) { try { return postFile(uploadFile[0].file, uploadFile[0].projectNo, uploadFile[0].filename); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); filesUploading --; } @Override public boolean isDone() { return AsyncTask.Status.FINISHED == getStatus(); } } 

这允许我分别发送每个图像,并单独处理它们。