使用http将文件发送到android中的服务器asp

时间:2016-06-03 15:31:49

标签: android asp-classic sendfile

服务器端

public class FileController : ApiController
{

    public HttpResponseMessage PostFile()
    {
        var httpRequest = HttpContext.Current.Request;
        if (httpRequest.Files.Count > 0)
        {
            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files[file];
                var filePath = HttpContext.Current.Server.MapPath("~/App_Data/Uploads/" + postedFile.FileName);
                postedFile.SaveAs(filePath);
            }

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}

客户端

private void sendFileCrashReport()
{
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            String url = "http://hostname/File/PostFile";
            File file = new File(MainActivity.this.getFilesDir().getAbsolutePath(),
                    "FileText.txt");
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(url);

                InputStreamEntity reqEntity = new InputStreamEntity(
                        new FileInputStream(file), -1);
                reqEntity.setContentType("binary/octet-stream");
                //reqEntity.setChunked(true); // Send in multiple parts if needed
                httppost.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(httppost);
                if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_CREATED)
                {
                    android.util.Log.i(TAG,"HttpCreated");
                }
                else {
                    android.util.Log.i(TAG,"HttpFail");
                }
                //Do something with response...

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();

}

我已经在Internal中有一个文件,我想把它发送到服务器。

response.getStatusLine()。getStatusCode()总是404,"找不到页面",我试过了:

http://hostname/File/PostFile
http://hostname/api/File/PostFile

任何人都知道我错了什么?或者那个网址里有什么东西? 我参考下面的两个链接:

Server Side

Android Side

0 个答案:

没有答案