#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());
}