private void ok(){
String file = Environment.getExternalStorageDirectory() + File.separator + "Pictures/a13.jpg";
final File sise = new File(file);
if (!sise.exists()) {
Toast.makeText(this, "文件损坏或路径错误", Toast.LENGTH_SHORT).show();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), sise);
MultipartBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("key", "1908A")
.addFormDataPart("file", sise.getName(), requestBody)
.build();
Request request = new Request.Builder()
.url("http://yun918.cn/study/public/file_upload.php")
.post(body)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final UpLoadBean upLoadBean = new Gson().fromJson(string, UpLoadBean.class);
final UpLoadBean.DataBean data = upLoadBean.getData();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, upLoadBean.getRes(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "run: " + upLoadBean.getRes());
Glide.with(MainActivity.this).load(data.getUrl()).into(Image);
}
});
}
});
}
OK上传文件
最新推荐文章于 2022-10-25 12:19:40 发布