图像上传问题从Android设备到.NET webservice

时间:2014-07-31 08:14:27

标签: android androidhttpclient

已经过了几天,我仍然坚持使用此图片上传功能,但我无法使用此功能。我不会上传我的任何源代码,因为它看起来很可怜​​而且不起作用所以我不得不求助一个可以让我为我工作的人。我已经谷歌搜索了一段时间,但似乎无法使它们中的任何一个工作。

基本上我要做的是将Bitmap上传到服务器,它必须作为fileStream发送,如下所示。这就是全部。

无论如何,这是Web服务的源代码。当从Android设备发出HttpPost请求时,这将被激活。请注意,这不是我的代码。其他人负责这个。

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;
   }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我就是这样做的,你当然是在后台任务中运行的

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "***1234321**";
    String filePath = "path/to/file";
    String fileName = new File(filePath).getName();
    StringBuffer response =new StringBuffer();

    try {
        URL connectUrl = new URL((String) params[0]);
        FileInputStream fileInputStream = new FileInputStream(filePath);
        HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\""+ params[2] +"\"; filename=\"" + fileName +"\"" + lineEnd);
        dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
        dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        // send multipart form data necessary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();
        dos.flush();


        dos.close();

    } catch (Exception e) {

    }