我是一周以来试图通过http上传文件到服务器。我找到了一些教程,但是我无法使用它们。一些类和方法被删除等等。现在我想自己做,但我需要知道步骤。
我知道手机上有一张我要上传的图片。我需要做什么步骤?
答案 0 :(得分:1)
将图像上传为二进制(字节数组)
这是您可以提供的代码:
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"Your_file");
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
}
catch (Exception e)
{
// show error
}