Okhttp图像文件是一个空数组

时间:2018-02-19 09:17:26

标签: php android laravel okhttp3

我正在将我的Android应用中的图像和相关数据发送到我的laravel php后端

final MediaType JPEG = MediaType.parse("image/JPEG; charset=utf-8");

RequestBody body = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("filename", image.getFilename())
            .addFormDataPart("hash", image.getHash())
            .addFormDataPart("job_id", Integer.toString(image.getJobId()))
            .addFormDataPart("team", Integer.toString(image.getTeam()))
            .addFormDataPart("type", image.getType())
            .addFormDataPart("image_file", image.getFilename(), RequestBody.create(JPEG, imagefile)).build();

    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();

    Response response = client.newCall(request).execute();

后端php看起来像这样

public function updateJobImage(array $request_data){
    var_dump($request_data);

    $toInsertImage = [
        'job_id'    => $request_data['job_id'],
        'type'      => $request_data['type'],
        'filename'  => $request_data['filename'],
        'team'      => $request_data['team'],
        'hash'      => $request_data['hash']
    ];

    $filepath = '/schedule_images/' . $request_data['hash'] . "/" . $request_data['filename'];

    Storage::disk('s3_upload')->put($filepath, file_get_contents($request_data['image_file']));

我可以完全获得所有job_id / type / filename / team / hash数据,但image_file是一个空数组。如何处理在php端获取图像文件

1 个答案:

答案 0 :(得分:0)

还请检查image.getfilename()是否存在

          final MediaType MEDIA_TYPE = new image.getfilename().endsWith("jpg") ?
                  MediaType.parse("image/png") : 
MediaType.parse("image/jpeg");


          RequestBody requestBody = new MultipartBuilder()
                  .type(MultipartBuilder.FORM)
                  .addFormDataPart("image_file", System.currentTimeMillis()+"profile.jpg", RequestBody.create(MEDIA_TYPE, image.getfilename()))

                  .build();

          Request request = new Request.Builder()
                  .post(requestBody)
                  .url(url)
                  .build();