Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t将文件从android应用程序上载到wep api_Java_C#_Android_File Upload_Asp.net Web Api - Fatal编程技术网

Java Can';t将文件从android应用程序上载到wep api

Java Can';t将文件从android应用程序上载到wep api,java,c#,android,file-upload,asp.net-web-api,Java,C#,Android,File Upload,Asp.net Web Api,我正在尝试使用java将一个图像文件从android应用程序上传到一个aspnet webapi,但没有成功。我总是收到500的回复码。下面是准备从我的android应用程序发送数据的基本代码。这段代码在调用php脚本时有效,但在调用如下所示的web api控制器时无效 FileInputStream fileInputStream = new FileInputStream(sourceFile); url = new URL("http://www.myserv

我正在尝试使用java将一个图像文件从android应用程序上传到一个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;
    }

我非常感谢您能帮助我判断代码的错误,这样我就可以将一个文件从android java应用程序上传到webapi控制器

连接是什么。getResponseCode();在第一行打开连接后?响应代码是500,这说明不了多少。它对web api程序中的第一条指令抛出异常:抛出新的HttpResponseException(HttpStatusCode.UnsupportedMediaType);文件扩展名是什么?访问本文:文件扩展名为.jpg。您引用的示例使用php脚本上载文件。我已经在用一个现有的php脚本做这件事了。我希望能够用WEPAPI做同样的事情。对于这个场景,似乎没有任何例子。http错误500没有特定的错误消息。这是一个一般性错误,您必须自己深入探究;在第一行打开连接后?响应代码是500,这说明不了多少。它对web api程序中的第一条指令抛出异常:抛出新的HttpResponseException(HttpStatusCode.UnsupportedMediaType);文件扩展名是什么?访问本文:文件扩展名为.jpg。您引用的示例使用php脚本上载文件。我已经在用一个现有的php脚本做这件事了。我希望能够用WEPAPI做同样的事情。对于这个场景,似乎没有任何例子。http错误500没有特定的错误消息。这是一个普遍的错误,你必须自己深入探究。
        config.Routes.MapHttpRoute(
           name: "FileUploadApi",
           routeTemplate: "api/{controller}/{action}/{name}", 
           defaults: new { action = "post", name = RouteParameter.Optional }
       );