Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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
在android中使用post方法将图像文件上载到Http服务器_Android_Http - Fatal编程技术网

在android中使用post方法将图像文件上载到Http服务器

在android中使用post方法将图像文件上载到Http服务器,android,http,Android,Http,我需要上传图像文件到http服务器使用post方法。 我的密码在这里 try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1); reqEntity.setContentType("

我需要上传图像文件到http服务器使用post方法。 我的密码在这里

try {
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

InputStreamEntity reqEntity = new InputStreamEntity(
        new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");

httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...

} catch (Exception e) {
// show error
}
没用了,给我建议正确的方法


注意:服务器是IIS服务器。

您可以尝试
reqEntity.setChunked(true)如果需要,发送多个部分

为此,您的代码应该是

InputStreamEntity reqEntity = new InputStreamEntity(
        new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
但是如果你的文件太大,那么你应该使用

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

File f = new File(file);
FileInputStream fileInputStream = new FileInputStream(f);
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, f.length());

httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

if (responseEntity != null) {
  responseEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();

几天前,我在这一点上也被绊倒了,什么都不能用。我正在使用WCF rest服务,并在其中花费了15-20天。最后我把我的WCF改成了webservice,现在我可以很容易地上传图片和呼叫服务了。我使用SOAP对象来调用它

private String RegisterUser(String[] parameter, String image) {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);

        request.addProperty("param1", parameter[0].toString());
        request.addProperty("param2", parameter[1].toString());
        request.addProperty("profilepic", image);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response = null;

        try {
            httpTransport.call(SOAP_ACTION + OPERATION_NAME, envelope);
            response = envelope.getResponse();
        } catch (Exception exception) {
            response = exception.toString();
        }
        return response.toString();
    }
我已经将图像转换为字符串,然后调用webservice,一切都非常顺利

如果愿意使用webservice,您可以尝试一下。希望这对你有帮助

问候,

Sourabh

它不工作。
可能是软件中最无用的语句。发布了错误的图像?你的应用程序崩溃了?没有图片发布?服务器崩溃了?太阳停止照耀了?我只上传了一张正确的图片。应用程序没有崩溃。在上传InputStreamEntity时,该实体具有文件数据。但在服务器端,文件数据为空。我认为我的评论有很多细节。如果可能的话,给我提供一些有用的信息。