您需要向服务器发送一个带有数据的 POST 请求,即 3 行和一个图像。我使用 OkHttp 库,这里是代码本身:
static void uploadImage() {
new Thread(new Runnable() {
@Override
public void run() {
try {
final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
RequestBody req = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("param1", "test1")
.addFormDataPart("param2", "test2")
.addFormDataPart("param3", "test3")
.addFormDataPart("param_img", "IMG_20200903_133732.jpg",
RequestBody.create(MEDIA_TYPE_JPEG, "storage/emulated/0/DCIM/Camera/IMG_20200903_133732.jpg"))
.build();
Request request = new Request.Builder()
.url("https://mysite.ru/test.php")
.post(req)
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
Log.d("response", "uploadImage:" + response.body().string());
} catch (UnknownHostException | UnsupportedEncodingException e) {
Log.e("testtest", "Error: " + e.getLocalizedMessage());
} catch (Exception e) {
Log.e("testtest", "Other Error: " + e.getLocalizedMessage());
}
}
}).start();
}
到目前为止,我会立即为手机上的特定图片指定一条特定路径以进行测试。结果,在服务器上,图片以这种形式出现,并没有打开,实际上不是图片:

剩下的数据(字符串)进来,被添加到数据库中,它们没有问题。这是接收的php文件,可能有问题:
<?php
include("config.php");
if(isset($_POST['param1']) && isset($_POST['param2']) && isset($_POST['param3'])){
$test111 = $_POST['param1'];
$test222 = $_POST['param2'];
$test333 = $_POST['param3'];
$uploaddir = '../img_message/';
$uploadfile = $uploaddir . basename($_FILES['param_img']['name']);
move_uploaded_file($_FILES['param_img']['tmp_name'], $uploadfile);
$query = mysqli_query($connection, "INSERT INTO test_table (one, two, three) VALUES ('$test111', '$test222', '$test333')");
}
mysqli_close($connection);
请告诉我可能是我的错误。
你的错误是
"storage/emulated/0/DCIM/Camera/IMG_20200903_133732.jpg"数据类型String,你需要发送数据类型File。那些。我不得不更换这条线:
在