我需要使用HttpGet Method在服务器上传图像文件。请帮帮我。 感谢
答案 0 :(得分:0)
使用http post request ...将图像转换为字节数组,然后将其发送到服务器
InputStream is = this.getAssets().open("image.png");
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest =
new HttpPost("http://webserver.com/doSomething.do");
byte[] data = IOUtils.toByteArray(is);
InputStreamBody isb = new InputStreamBody(new
ByteArrayInputStream(data), "uploadedFile");
StringBody sb1 = new StringBody("some text goes here");
StringBody sb2 = new StringBody("some text goes here too");
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("uploadedFile", isb);
multipartContent.addPart("one", sb1);
multipartContent.addPart("two", sb2);
postRequest.setEntity(multipartContent);
HttpResponse response =httpClient.execute(postRequest);
response.getEntity().getContent().close();
答案 1 :(得分:0)
正如Nitesh已经暗示的那样,为了上传图像文件,您应该使用Post请求并将文件作为二进制文件提交。
有关详细信息,请参阅SO中的以下链接: How do I send a file in Android from a mobile device to server using http?
答案 2 :(得分:0)
试试这个
String _URL =https://194.1.3.9:7721/request?image_byte=iamge _inbyte;
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(_URL);
HttpResponse response = client.execute(request);
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}