如何从android安装远程服务器上的图像

时间:2011-01-14 12:57:23

标签: java android image http

我尝试使用以下代码,但它无效

File file= new File(filename);
byte[] data = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://www.example.com/resource");
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);
for (int i = 0; i  pairs = new ArrayList();
pairs.add(new BasicNameValuePair("Data", data));
put.setEntity(new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(put);

我无法在服务器上输出数据,我也尝试过使用HttpsURLConnection,但它已上传。

1 个答案:

答案 0 :(得分:0)

设置内容长度后,我可以在HttpURLConnection的帮助下执行HTTP put,如下所示

https.setRequestProperty("Content-Type", "image/jpeg");
https.setRequestProperty("Content-Length", "" + file.length());