private void uploadFile(String path) {
File file = new File(path);
RequestBody requestBody = MultipartBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("pic", file.getName(), requestBody).build();
Request aa = new Request.Builder()
.url("http://192.168.202.55:8999/uploadPic")
.post(body)
.build();
Log.d("ning", "进来了");
//异步请求
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response = okHttpClient.newCall(aa).execute();
Log.d("ning", "执行了");
ResponseBody body = response.body();
if (body != null) {
String s = body.string();
Log.d("ning", s);
runOnUiThread(() -> {
binding.text.setText(s);
});
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
文件上传实现
最新推荐文章于 2024-05-18 16:42:38 发布