栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android中实现OkHttp上传文件到服务器并带进度

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android中实现OkHttp上传文件到服务器并带进度

在上一讲中 OkHttp下载文件并带进度条 中,我们知道怎样去下载文件了。那上传文件呢

一、编写服务器端

在上一讲服务器下新建UploadFileServlet,代码如下:然后重启服务器!

@WebServlet("/UploadFileServlet")
@MultipartConfig
public class UploadFileServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;


  public UploadFileServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    this.doPost(request, response);
  }

  
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    System.out.println("doPost==");
    request.setCharacterEncoding("utf-8");
    //获取file命名的part,注意要与Android端一样
    Part part = request.getPart("file");
    // 获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip"
    String header = part.getHeader("content-disposition");
    System.out.println(header);
    String fileName = getFileName(header);
    // 存储路径
    String savePath = "D:/huang/upload";
    // 把文件写到指定路径
    part.write(savePath + File.separator + fileName);


    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    writer.print("上传成功");
  }



  public String getFileName(String header) {
    
    String[] tempArr1 = header.split(";");
    
    String[] tempArr2 = tempArr1[2].split("=");
    // 获取文件名,兼容各种浏览器的写法
    String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\") + 1).replaceAll(""", "");
    return fileName;
  }

}

二、Android端

1.布局,上一讲activity_main代码中添加 :

 

 

2.OkHttpUtil新增上传文件方法:

 public static void postFile(String url, final ProgressListener listener, Callback callback, File...files){

    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    Log.i("huang","files[0].getName()=="+files[0].getName());
    //第一个参数要与Servlet中的一致
    builder.addFormDataPart("file",files[0].getName(), RequestBody.create(MediaType.parse("application/octet-stream"),files[0]));

    MultipartBody multipartBody = builder.build();

    Request request = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody,listener)).build();
    okHttpClient.newCall(request).enqueue(callback);
  }

3.ProgressRequestBody是自定义RequestBody类,用来监听进度:

public class ProgressRequestBody extends RequestBody {
  public static final int UPDATE = 0x01;
  private RequestBody requestBody;
  private ProgressListener mListener;
  private BufferedSink bufferedSink;
  private MyHandler myHandler;
  public ProgressRequestBody(RequestBody body, ProgressListener listener) {
    requestBody = body;
    mListener = listener;
    if (myHandler==null){
      myHandler = new MyHandler();
    }
  }

  class MyHandler extends Handler {
  //放在主线程中显示 
    public MyHandler() {
      super(Looper.getMainLooper());
    }

    @Override
    public void handleMessage(Message msg) {
      switch (msg.what){
 case UPDATE:
   ProgressModel progressModel = (ProgressModel) msg.obj;
   if (mListener!=null)mListener.onProgress(progressModel.getCurrentBytes(),progressModel.getContentLength(),progressModel.isDone());
   break;

      }
    }


  }

  @Override
  public MediaType contentType() {
    return requestBody.contentType();
  }

  @Override
  public long contentLength() throws IOException {
    return requestBody.contentLength();
  }

  @Override
  public void writeTo(BufferedSink sink) throws IOException {

    if (bufferedSink==null){
      bufferedSink = Okio.buffer(sink(sink));
    }
    //写入
    requestBody.writeTo(bufferedSink);
    //刷新
    bufferedSink.flush();
  }

  private Sink sink(BufferedSink sink) {

    return new ForwardingSink(sink) {
      long bytesWritten = 0L;
      long contentLength = 0L;
      @Override
      public void write(Buffer source, long byteCount) throws IOException {
 super.write(source, byteCount);
 if (contentLength==0){
   contentLength = contentLength();
 }
 bytesWritten += byteCount;
 //回调
 Message msg = Message.obtain();
 msg.what = UPDATE;
 msg.obj = new ProgressModel(bytesWritten,contentLength,bytesWritten==contentLength);
 myHandler.sendMessage(msg);
      }
    };
  }


}

4.在MainActivity添加上传按钮点击事件,代码如下:

  File file = new File(basePath + "/1.mp4");
 String postUrl = "http://192.168.0.104:8080/OkHttpServer/UploadFileServlet";

 OkHttpUtil.postFile(postUrl, new ProgressListener() {
   @Override
   public void onProgress(long currentBytes, long contentLength, boolean done) {
     Log.i(TAG, "currentBytes==" + currentBytes + "==contentLength==" + contentLength + "==done==" + done);
     int progress = (int) (currentBytes * 100 / contentLength);
     post_progress.setProgress(progress);
     post_text.setText(progress + "%");
   }
 }, new Callback() {
   @Override
   public void onFailure(Call call, IOException e) {

   }

   @Override
   public void onResponse(Call call, Response response) throws IOException {
     if (response != null) {
String result = response.body().string();
Log.i(TAG, "result===" + result);
     }
   }
 }, file);

相关效果图:

 

上传完成后,在电脑D:huangupload下可以看到:

源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号