将文件从android上传到wcf服务

时间:2013-02-21 05:14:06

标签: android asp.net wcf file-upload

我在wcf结束时遇到问题,而我正在从android向wcf服务发送doc文件。问题是在wcf结束时打开上传的文件时,doc文件的内容是一些不可读的文本。以下是代码: wcf代码

FileStream fileToupload = new FileStream("D:\\myfile.doc", FileMode.Create, FileAccess.Write);

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

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

安卓代码

 package com.example.filedemo;


import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.os.Environment;

public class HttpUpload {

    public static int res ;

    public void myUploadedfile(){


        try {

            HttpParams httpParameters = new BasicHttpParams();
            HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
            HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
            HttpClient httpclient = new DefaultHttpClient(httpParameters);


            HttpPost httpost = new HttpPost("http://10.160.0.18:85/Service.svc/UploadImage?fn=abc.doc");
            httpost.setHeader("Content-type","application/octet-stream");

            MultipartEntity entity = new MultipartEntity();
            entity.addPart("fileContents", new FileBody(new File(Environment.getExternalStorageDirectory(),"abc.doc")));


            httpost.setEntity(entity);
            HttpResponse response;

            response = httpclient.execute(httpost);

            System.out.println("Response : "+response.getStatusLine().getStatusCode());

            res = response.getStatusLine().getStatusCode();


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: "+ e.getMessage());
            // TODO: handle exception
        }

    }

}

请尽快回复。

由于

1 个答案:

答案 0 :(得分:0)

WCF中的逻辑似乎不正确,请尝试以下修改:

FileStream fileToupload = new FileStream("D:\\myfile.doc", FileMode.Create, FileAccess.Write);

byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;

while((bytesRead = mystream.Read(bytearray)) > 0) 
{    
  fileToupload.Write(bytearray, 0, bytesRead); // Write directly to the file
  totalBytesRead += bytesRead;
} 


fileToupload.Close();
fileToupload.Dispose();