如何从Android的多张图片发送到WCF REST服务作为流写入到网络驱动器?发送到、驱动器、多张、图片

2023-09-07 02:12:22 作者:有深度的文艺

很多谷歌搜索和搜索后,我设法利用Android的multiparsers的图像发送到我的WCF服务,但理想情况下,我想一次发送多个图像,而不是遍地调用方法再次,因为它倒是要花很多时间,并添加了一堆更多的开销。

After much googling and searching, I managed to send an image using multiparsers from android to my WCF service, but ideally, I'd like to send several images at once, instead of calling the method over and over again, since it'd take a lot longer, and add a bunch more overhead.

这是我目前的code

This is my current code

的Andr​​oid(由code线发现这里的某个地方):

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#的时刻之一已经之一。

I'd like to go from sending one image, to sending several, each with 2 text attributes; the filename, and the project number they're associated with. Essentially, both is what I need it for. At the moment, I'm just trying to do put another addPart on to the android bit, but then I don't know how to add metadata to that and I wouldn't know how to parse it based on the name. I'm fine with using any third party libraries, the one I'm using on C# at the moment is already one.

非常感谢!

推荐答案

而不是在一件事发送多张图片,我只是坚持它在异步类,并在同一时间为10的最大送他们兼任,直到所有图像在该特定会话被发送。似乎做工精细,实现是一样的,所以我没有更改任何的是,这是很好的。如果有人想我张贴code我没有做到这一点,我很乐意。在这里只是小片段,并没有加入到上述code,虽然。

Instead of sending multiple images in one thing, I just stuck it in an asynchronous class and sent them concurrently with a max of 10 at a time until all the images are sent in that particular session. Seems to work fine, the implementation's the same, so I've not had to change any of that, which is good. If anyone would like me to post the code I did to do that, I'd be happy to. It's just small snippets here and there added to the above code, though.

好吧,我加入了主要的1位是:

Well, the main bit I added was:

public static class FileUploader extends AsyncTask<UploadFile , Void , String> implements Future<String>
{
    @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();
    }


}

这让我可以每个图像分别发送,并分别处理它们。

This allows me to send each image separately, and handle them separately.