Android OkhttpUtils上传图片和文件
2017-03-15 17:41
417 查看
#okhttp的使用-单个文件的上传
/**
* 上传文件
* @param view
*/
public void uploadFile(View view)
{
File file = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if (!file.exists())
{
Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show();
return;
}
Map<String, String> params = new HashMap<>();
// params.put("username", "杨光福");
// params.put("password", "123");
Map<String, String> headers = new HashMap<>();
headers.put("APP-Key", "APP-Secret222");
headers.put("APP-Secret", "APP-Secret111");
String url = "http://192.168.10.168:8080/FileUpload/FileUploadServlet";
OkHttpUtils.post()//
.addFile("mFile", "agguigu-afu.jpe", file)//
.url(url)//
.params(params)//
.headers(headers)//
.build()//
.execute(new MyStringCallback());
}
#okhttp的使用-多个文件的上传
/**
* 多文件同时上传
* @param view
*/
public void multiFileUpload(View view)
{
File file = new File(Environment.getExternalStorageDirectory(), "1.jpg");
File file2 = new File(Environment.getExternalStorageDirectory(), "2.txt");
if (!file.exists()||!file2.exists())
{
Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show();
return;
}
Map<String, String> params = new HashMap<>();
// params.put("username", "杨光福");
// params.put("password", "123");
String url = "http://192.168.10.168:8080/FileUpload/FileUploadServlet";
OkHttpUtils.post()//
.addFile("mFile", "01.jpg", file)//
.addFile("mFile", "afua.txt", file2)//
.url(url)
.params(params)//
.build()//
.execute(new MyStringCallback());
}
/**
* 上传文件
* @param view
*/
public void uploadFile(View view)
{
File file = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if (!file.exists())
{
Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show();
return;
}
Map<String, String> params = new HashMap<>();
// params.put("username", "杨光福");
// params.put("password", "123");
Map<String, String> headers = new HashMap<>();
headers.put("image", new File(path));
String url = "http://192.168.10.168:8080/FileUpload/FileUploadServlet";
OkHttpUtils.post()//
.url(url)//
.params(params)//
.headers(headers)//
.build()//
.execute(new MyStringCallback());
}
相关文章推荐
- Android okhttp上传图片文件
- Android 使用OKhttp 3.0以上版本上传文件或图片(MultipartBuilder已不可用)
- Android Okhttp框架以文件方式上传图片
- android 使用OkHttp上传多张图片
- Android 图片(文件--带参数)批量上传到服务器
- 【Android】Android客户端使用okhttp上传文件php服务器
- android okhttp3中设置请求头以及上传图片
- Android使用put上传图片和所有类型的文件
- Android上传图片文件工具类
- Android okHttp图片上传
- android利用 MultipartEntity同时上传图片,文件和内容小结
- android批量文件上传(android批量图片上传)
- Android Retrofit实现多图片/文件、图文上传功能
- android okHttp3 post上传图片给服务器
- Android头像(图片)上传/文件上传
- **张鸿洋**封装的okhttputils 上传文字加图片 实现发表朋友圈效果
- android中的文件(图片)上传
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- android上传文件到服务器(图片、文本等)
- android 使用OkHttp上传多张图片的实现代码