首页 > 解决方案 > 当我将文件上传到服务器时,如何获取文件名?

问题描述

当我将文件上传到服务器时,如何获取文件名?

我用这个方法

Map<String, RequestBody> map = new HashMap<>();
            //File file = new File(newvideoPath);
            String fullFilePath = PathUtil.getPathFromUri(this,contentURI);
            // Parsing any Media type file
            File file = new File(fullFilePath);
            RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
            map.put("file\"; filename=\"" + file.getName() + "\"", requestBody);
            ApiConfig getResponse = AppConfig.getApiClient().create(ApiConfig.class);
            Call<ServerResponse> call1 = getResponse.upload("token", map);
            call1.enqueue(new Callback<ServerResponse>() {
                @Override
                public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                    if (response.isSuccessful()){
                        if (response.body() != null){
                            progressDialog.dismiss();
                            ServerResponse serverResponse = response.body();
                            Toast.makeText(getApplicationContext(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(UploadActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }else {

                        Toast.makeText(getApplicationContext(), "problem uploading video", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ServerResponse> call, Throwable t) {

                    Log.v("Response gotten is", t.getMessage());
                    Toast.makeText(getApplicationContext(), "problem uploading video " + t.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });

这是php代码

$target_dir = "uploads/";  
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);  
$response = array(); 

$video_path = basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name)

我可以将文件上传到服务器,但我无法获取文件名

如果文件名为111.MOV,则上传成功,可以在uploads forder中找到111.MOV。

但是,如何获取文件名?

标签: phpandroiduploadokhttp

解决方案


推荐阅读