使用改造2进行分段上传失败

时间:2017-11-03 07:31:46

标签: java php android retrofit

我是改装新手,我想使用改装上传2.每个上传文件的响应都是

  

无法上传

来自php但是如果我使用Postman它总是成功。

以下是我的代码。

主要活动

File zip = new File(Environment.getExternalStorageDirectory() + "/test.zip");
            RequestBody reqBody = RequestBody.create(MediaType.parse("multipart/form-file"), zip);
            MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", zip.getName(), reqBody);

            ApiServices api = RetroClient.getApiServices();
            Call<ResponseApiModel> upload = api.fileUpload(filePart);
            upload.enqueue(new Callback<ResponseApiModel>() {
                @Override
                public void onResponse(Call<ResponseApiModel> call, Response<ResponseApiModel> response) {

                    if (response.body().getCode().equals("1")) {
                        Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();

                    }
                }

                @Override
                public void onFailure(Call<ResponseApiModel> call, Throwable t) {
                    Log.d("RETRO", "ON FAILURE : " + t.getMessage());
                }
            });

Api服务

@Multipart
@POST("getzip.php")
Call<ResponseApiModel> fileUpload (@Part MultipartBody.Part File);

PHP代码

$part = "./upload/";
$filename = rand(9,9999).".zip";

$res = array();
$code = "";
$message = "";


if($_SERVER['REQUEST_METHOD'] == "POST")
{
    if(isset($_FILES['file'])){
        $destinationfile = $part.$filename;
        $data = $_FILES['file'];
        if(move_uploaded_file($data['tmp_name'], $destinationfile)) { 
            $code = 1;
            $message = "Success Upload";
        }else {
            $code = 0;
            $message = "Fail Upload";
            }
    }else{
        $code = 0;
        $message = "request error";
    }
}else
{
    $code = 0;
    $message = "Request Not Vaild";
}

$res['code'] = $code;
$res['message'] = $message;

echo json_encode($res);

2 个答案:

答案 0 :(得分:1)

我不懂PHP,但我知道Retrofit。这是我如何做到并完美运行,这会上传整个文件夹,您可以相应更改以处理您的文件。

您需要使用多部分格式。

以下是代码示例:

    @Multipart
    @POST("sync/contact/image")
    Call<Response> ImageUpload(@Part MultipartBody.Part file);

    @Multipart
    @POST("sync/image")
    Call<ResponseBody> MultiImageUpload(@PartMap() Map<String, RequestBody> mapFileAndName);


 public static HashMap<String, RequestBody> GetAllImage(Context context) {
        File files = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/.ELMEX");
        File[] filesArray = files.listFiles();
        List<File> listOfNames = Arrays.asList(filesArray);
        HashMap<String, RequestBody> map = new HashMap<>(listOfNames.size());
        RequestBody file = null;

        for (int i = 0, size = listOfNames.size(); i < size; i++) {
            file = RequestBody.create(MediaType.parse("multipart/form-data"), listOfNames.get(i));
            map.put("file\"; filename=\"" + listOfNames.get(i).getName() + ".jpg", file);
            file = null;
        }

        return  map;
    }


 HashMap<String, RequestBody> map = UtilImage.GetAllImage(context);

        Call<ResponseBody> call = Retro.getRetroWS().MultiImageUpload(map);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d(TAG, "onResponse: ");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
            }
        });

答案 1 :(得分:0)

您可以查看in this answer给出的所有建议。查看所有答案和评论,而不仅仅是接受的答案,因为对于可能出现的问题有一些非常好的想法,以及如何调试问题的好主意。