android httpclient 上传文件

简介:
public void uploadFileClient()
  {
    Toast.makeText(this, "现在已经开始上传了!", Toast.LENGTH_LONG).show();
    String targetURL = actionUrl;// 上传指定URL

    File targetFile = new File(uploadFile);// 指定上传文件

    PostMethod filePost = new PostMethod(targetURL);

    try
    {

      // 通过以下方法可以模拟页面参数提交
      // filePost.setParameter("name", "中文");
      // filePost.setParameter("pass", "1234");
      byte[] buffer = new byte[1024];
      Part[] parts =
      { new FilePart(targetFile.getName() + System.currentTimeMillis(),
          targetFile) };

      filePost.setRequestEntity(new MultipartRequestEntity(parts,
          filePost.getParams()));
      HttpClient client = new HttpClient();
      client.getHttpConnectionManager().getParams()
          .setConnectionTimeout(5000);
      int status = client.executeMethod(filePost);

      if (status == HttpStatus.SC_OK)
      {
        System.out.println("上传成功");
        // 上传成功
      } else
      {
        System.out.println("上传失败");
        // 上传失败
      }
    } catch (Exception ex)
    {
      ex.printStackTrace();
    } finally
    {
      filePost.releaseConnection();
    }

  }

相关文章
|
PHP Android开发
android通过http上传文件,服务器端用php写(原创)
android通过http上传文件,服务器端用php写(原创)
127 4
|
前端开发 Java API
Android端通过HttpURLConnection上传文件到服务器
Android端通过HttpURLConnection上传文件到服务器
216 0
|
XML Java Android开发
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
848 0
|
Apache
Android-上传图片(二)_HttpClient
Android-上传图片(二)_HttpClient
207 0
|
Java Apache Android开发

热门文章

最新文章