无法将文件从Android应用程序上传到wep api

时间:2015-09-26 15:29:07

标签: java c# android file-upload asp.net-web-api

我正在尝试使用java将图像文件从使用java的web应用程序上传到aspnet webapi但没有成功。我总是得到500的响应代码。以下是准备从我的Android应用程序发送数据的基本代码。这段代码在调用php脚本时有效,但在我调用下面显示的web api控制器时则无效。

   FileInputStream fileInputStream = new FileInputStream(sourceFile);

           url = new URL("http://www.myserver.com/webapi/api/fileupload/post/thisfile");
           connection = (HttpURLConnection) url.openConnection();
            // Allow Inputs & Outputs.
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            // Set HTTP method to POST.
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + sourceFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // Responses from the server (code and message)
            serverResponseCode = connection.getResponseCode();

以下是执行文件上传的基本web api控制器代码:

 [HttpPost]
 [AcceptVerbs("GET", "POST")]
 public Task<HttpResponseMessage> Post()
    {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                //    return "Unsupported media type";
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string root = "e:/web/myserver/htdocs/cemetery/pics/";
            var provider = new MultipartFormDataStreamProvider(root);

       return task;
    }

我更改了上面的代码以发回一个字符串,它说我的请求不包含multipart / form-data。下面是我的webapiconfig.cs文件路径。

        config.Routes.MapHttpRoute(
           name: "FileUploadApi",
           routeTemplate: "api/{controller}/{action}/{name}", 
           defaults: new { action = "post", name = RouteParameter.Optional }
       );

我很感谢帮助确定我的代码有什么问题所以我可以将一个文件从android java app上传到webapi控制器。

0 个答案:

没有答案