简体   繁体   English

Android中将图片上传到.Net Web服务

[英]image upload in Android to .Net webservice

I've been working with this Android app that uploads image on a server. 我一直在使用此Android应用程序在服务器上上传图像。 I was able to upload successfully using PHP but I'm having trouble uploading it on a .net webservice. 我可以使用PHP成功上传,但是无法在.net网络服务上上传。 The guys who is in charge of the webservice gave the code to me so I can have a looked at it. 负责Web服务的家伙把代码给了我,所以我可以看一看。

Here it is. 这里是。

public Stream FileUpload(string fileName, Stream fileStream)
        {
            var serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FileUpload/");
            if (File.Exists(serverPath + fileName)) File.Delete(serverPath + fileName); // delete file if already used


            //FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
            FileStream fileToupload = new FileStream(serverPath + fileName, FileMode.Create);

            byte[] bytearray = new byte[10000];//
            int bytesRead, totalBytesRead = 0;
            do
            {
                bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                totalBytesRead += bytesRead;
            } while (bytesRead > 0);

            fileToupload.Write(bytearray, 0, bytearray.Length);
            fileToupload.Close();
            fileToupload.Dispose();

            FileStream fs = File.OpenRead(serverPath + fileName);
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
            return fs;

        }

Problem is I don't have experience in .net so I'm not sure how to handle this situation. 问题是我没有.net经验,所以我不确定如何处理这种情况。 It seems the upload image function in the webservice use a fileStream as you can see from the parameters above. 从上面的参数可以看到,似乎Web服务中的上载图像功能使用了fileStream。

EDIT 编辑

Here's my java code. 这是我的Java代码。

HttpResponse httpResponse = null;
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(new File(filePath));
            byte[] data;
            try {
                data = IOUtils.toByteArray(inputStream);

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(
                        "http://localhost/fileUpload");

                InputStreamBody inputStreamBody = new InputStreamBody(
                        new ByteArrayInputStream(data), fileName);
                MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                        .create();
                multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntity.addPart("file", inputStreamBody);
                HttpEntity entity = multipartEntity.build();
                httpPost.setEntity(entity);
                httpResponse = httpClient.execute(httpPost);

                if (httpResponse != null) {

                } else {

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

I found a tutorial here . 我在这里找到了一个教程。 I wasn't successful in uploading the file but that's for another question. 我上传文件失败,但这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM