簡體   English   中英

WCF服務使用來自Android的Multipart數據

[英]WCF service to consume Multipart data from android

我正在使用android okhttp發送多部分數據,即圖像和文本到wcf服務。 我可以使用此處提到的自定義MultipartParser類在服務器上發送和解析部分數據。

當我的文件寫入服務器時,由於Multipart數據中的某些不需要的字符而用字節寫入了文件,因此損壞了[我通過在notepad ++中打開顯示編碼的字節和不需要的字符的圖像發現了這一點]。 我可以通過為contentLenMatch添加正則表達式匹配器來刪除這些內容,但仍在文件開頭寫入換行符,因此顯示為損壞。

在將數據寫入文件之前,我需要知道如何從數據中刪除空格或\\ n。

代碼:

     public class MultipartParser    {
     public IDictionary<string, string> Parameters = new Dictionary<string, string>();
     public MultipartParser(Stream stream)        {
        this.Parse(stream, Encoding.UTF8);
    }
    public MultipartParser(Stream stream, Encoding encoding)        {
        this.Parse(stream, encoding);
    }
    public string getcontent(Stream stream, Encoding encoding)        {
        byte[] data = ToByteArray(stream);
        string content = encoding.GetString(data);
        string delimiter = content.Substring(0, content.IndexOf("\r\n"));
        string[] sections = content.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in sections)       {
            Match nameMatch = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s);
            string name = nameMatch.Value.Trim().ToLower();
            if (!string.IsNullOrWhiteSpace(name))        {
                int startIndex = nameMatch.Index + nameMatch.Length + "\r\n\r\n".Length;    
            }            
        string strRet = ""; //Parameters["name"];
        return strRet;
    }
    private void Parse(Stream stream, Encoding encoding)        {
        this.Success = false;
        byte[] data = ToByteArray(stream);
        string content = encoding.GetString(data);
        int delimiterEndIndex = content.IndexOf("\r\n");
        if (delimiterEndIndex > -1)    {
            string delimiter = content.Substring(0, content.IndexOf("\r\n"));
            Regex re = new Regex(@"(?<=Content\-Type:)(.*?)");
            Match contentTypeMatch = re.Match(content);
            re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")");
            Match filenameMatch = re.Match(content);


            // to match Content-Length and remove ***Unwanted chars***
            re = new Regex(@"(?<=Content\-Length:)(.*)");
            Match contentLenMatch = re.Match(content);
            if(contentLenMatch.Success){
                contentLenMatch = contentLenMatch.NextMatch();
            }


            if (contentTypeMatch.Success && filenameMatch.Success)    {
                this.ContentType = contentTypeMatch.Value.Trim();
                this.Filename = filenameMatch.Value.Trim();


                // changed from contentTypeMatch to contentLenMatch
                // startIndex must point to the 1st byte 
                // from where the file needs to be written
                // Need to remove extra \n that gets written to the file
                int startIndex = contentLenMatch.Index + contentLenMatch .Length + "\r\n\r\n".Length;


                byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter);
                int endIndex = IndexOf(data, delimiterBytes, startIndex);
                int contentLength = endIndex - startIndex;
                byte[] fileData = new byte[contentLength];
                Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength);
                this.FileContents = fileData;
                this.Success = true;                }
        }        }
    private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)       {
        int index = 0;
        int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex);
        if (startPos != -1)          {
            while ((startPos + index) < searchWithin.Length)             {
                if (searchWithin[startPos + index] == serachFor[index])                   {
                    index++;
                    if (index == serachFor.Length)                        {
                        return startPos;
                    }
                }    else      {
                    startPos = Array.IndexOf<byte>(searchWithin, serachFor[0], startPos + index);
                    if (startPos == -1)      {
                       return -1;
                    }
                    index = 0;
                }
            }
        }
        return -1;
    }
    private byte[] ToByteArray(Stream stream)        {
        byte[] buffer = new byte[32768];
        using (MemoryStream ms = new MemoryStream())           {
            while (true)       {
                int read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }
    public bool Success        {
        get;
        private set;
    }
    public string ContentType        {
        get;
        private set;
    }
    public string Filename        {
        get;
        private set;
    }
    public byte[] FileContents        {
        get;
        private set;
    }
    public string Imgname        {
        get;
        private set;
    }    

好的,我很早就通過一些嘗試和錯誤的方式解決了這個問題,但是由於沒有人回答,所以我發布了我的解決方案。

public class MultipartParser
{
    public IDictionary<string, string> Parameters = new Dictionary<string, string>();
    public MultipartParser(Stream stream)
    {
        this.Parse(stream, Encoding.UTF8);
    }
    public MultipartParser(Stream stream, Encoding encoding)
    {
        this.Parse(stream, encoding);
    }
    public string getcontent(Stream stream, Encoding encoding)
    {
        // Read the stream into a byte array
        byte[] data = ToByteArray(stream);
        // Copy to a string for header parsing
        string content = encoding.GetString(data);
        string delimiter = content.Substring(0, content.IndexOf("\r\n"));
        string[] sections = content.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in sections)
        {
            Match nameMatch = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s);
            string name = nameMatch.Value.Trim().ToLower();
            if (!string.IsNullOrWhiteSpace(name))
            {
                int startIndex = nameMatch.Index + nameMatch.Length + "\r\n\r\n".Length;
            }
        }
        string strRet = ""; //Parameters["name"];
        return strRet;
    }
    private void Parse(Stream stream, Encoding encoding)
    {
        this.Success = false;            
        byte[] data = ToByteArray(stream);            
        string content = encoding.GetString(data);

        string s_no = content.Substring(content.IndexOf("s_no"), 100);                       
        string[] lines = s_no.Split(new[] { "\r\n", "\r\n\r\n" }, StringSplitOptions.None);
        this.S_NO = lines[3];            

        string count = content.Substring(content.IndexOf("Count"), 100);            
        string[] linescount = count.Split(new[] { "\r\n", "\r\n\r\n" }, StringSplitOptions.None);
        this.Count = linescount[3];   

        int delimiterEndIndex = content.IndexOf("\r\n");
        if (delimiterEndIndex > -1)
        {
            string delimiter = content.Substring(0, content.IndexOf("\r\n"));
            // Look for Content-Type
            Regex re = new Regex(@"(?<=Content\-Type:)(.*?)");
            Match contentTypeMatch = re.Match(content);
            // Look for filename
            re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")");
            Match filenameMatch = re.Match(content);
            re = new Regex(@"(?<=Content\-Length:)(.*)");
            Match contentLenMatch = re.Match(content);

            if (contentLenMatch.Success)
            {
                contentLenMatch = contentLenMatch.NextMatch();
            }
            if (contentLenMatch.Success)
            {
                contentLenMatch = contentLenMatch.NextMatch();
            }
            //re = new Regex(@"(?<=name\=\"")(.*?)(?=\"")");
            //Match nameMatch = re.Match(content);
            // Did we find the required values?
            if (contentTypeMatch.Success && filenameMatch.Success)
            {
                // Set properties
                this.ContentType = contentTypeMatch.Value.Trim();
                this.Filename = filenameMatch.Value.Trim();
                // Get the start & end indexes of the file contents
                int startIndex = contentLenMatch.Index + contentLenMatch.Length + "\r\n".Length + 1;
                byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter);
                int endIndex = IndexOf(data, delimiterBytes, startIndex);
                int contentLength = endIndex - startIndex;
                // Extract the file contents from the byte array
                byte[] fileData = new byte[contentLength];
                Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength);
                this.FileContents = fileData;
                this.Success = true;
            }
        }
    }
    private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)
    {
        int index = 0;
        int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex);
        if (startPos != -1)
        {
            while ((startPos + index) < searchWithin.Length)
            {
                if (searchWithin[startPos + index] == serachFor[index])
                {
                    index++;
                    if (index == serachFor.Length)
                    {
                        return startPos;
                    }
                }
                else
                {
                    startPos = Array.IndexOf<byte>(searchWithin, serachFor[0], startPos + index);
                    if (startPos == -1)
                    {
                        return -1;
                    }
                    index = 0;
                }
            }
        }
        return -1;
    }
    private byte[] ToByteArray(Stream stream)
    {
        byte[] buffer = new byte[32768];
        using (MemoryStream ms = new MemoryStream())
        {
            while (true)
            {
                int read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }
    public bool Success
    {
        get;
        private set;
    }
    public string ContentType
    {
        get;
        private set;
    }
    public string Filename
    {
        get;
        private set;
    }
    public byte[] FileContents
    {
        get;
        private set;
    }
    public string Imgname
    {
        get;
        private set;
    }
    public string S_NO
    {
        get;
        private set;
    }
    public string Count
    {
        get;
        private set;
    }
}
// End of Wcf rest Service Code

我的Android代碼如下所示,使用Okhttp

public static ResultVO uploadIMGMultipart(String s_no, int noOfDocs, String filepath, String url) {
    ResultVO resultVO = new ResultVO();
    OkHttpClient client = new OkHttpClient().newBuilder()
            .retryOnConnectionFailure(false)
            .connectTimeout(10000, TimeUnit.MILLISECONDS)
            .build();

    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("s_no",s_no.toString())
            .addFormDataPart("Count", ""+noOfDocs)
            .addFormDataPart("filestream", new File(filepath).getName().toString(), RequestBody.create(MEDIA_TYPE_PNG, new File(filepath)))
            .build();

    Request request = new Request.Builder()
            .url(url)
            .addHeader("content-type", "multipart/form-data;")
            .cacheControl(CacheControl.FORCE_NETWORK)
            .post(requestBody)
            .build();

    Response response = null;
    try {
        response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        resultVO.setStatus(response.code());
        resultVO.setResult(response.body().string());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return resultVO;
}

從類中調用AsyncTask中的上述方法。

干得好…

MyService.cs

[OperationContract]
[WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "/UPLOAD_DOCUMENTS")]
    string UPLOAD_DOCUMENTS(Stream fileStream);

IMyService.svc.cs

public string UPLOAD_DOCUMENTS(Stream fileStream) 
{
    string ResponseMessage = "Documents Not Uploaded";  
    string strRet = string.Empty;
    string strpdffile = string.Empty;
    string VAR_CLAIM_INWARD_NO = "";
    string response = string.Empty;
    int i = 0;
    string tempFolderPath = string.Empty;
    string UPLOADED_BY = string.Empty;
    string VAR_NEXT_FILE_NO = "";
    string s_no = string.Empty;
    string count = string.Empty;

    try
    {
        MultipartParser parser = new MultipartParser(fileStream);
        if (parser.Success)
        {
            string fileName = parser.Filename;
            string contentType = parser.ContentType;
            byte[] fileContent = parser.FileContents;
            Encoding encoding = Encoding.UTF8;
            s_no = parser.S_NO;
            count = parser.Count;

            string savepathImg = @"Your path" + fileNameToBeSaved + ".jpg";
            FileStream fileToupload = new FileStream(savepathImg, FileMode.Create);
            fileToupload.Write(fileContent, 0, fileContent.Length);
            fileToupload.Close();
            fileToupload.Dispose();
            fileStream.Close();
            strRet = fileName;
            tempFolderPath = @"Your path";                                              

        }
        else
        {

        }
    }
    catch (Exception ex)
    {

    }

    return ResponseMessage;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM