android 文件上传

时间:2023-01-02 21:10:44

1.java原生上传

  拼接上传的字符串

2.HttpClient方式上传

  1.导入httpClient jar(core、mime)包

  2.设置FileBody、MultiPartEntity、发送请求

  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(url);
  MultipartEntity muti = new MultipartEntity();
  File parent = Environment.getExternalStorageDirectory();
  File filAbs = new File(parent,"Sky.jpg");   FileBody fileBody = new FileBody("fileAbs");
  muti.addPart("file",fileBody);
  post.setEntity(muti);   HttpResponse response = client.execute(post);
  if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
    System.out.println(EntityUtils.toString(response.getEntity()));
  }