Retrofit - Multipart Request: Required Multipartfile Parameter 'File' Is Not Present

Retrofit - Multipart request: Required MultipartFile parameter 'file' is not present

You can try the following sample code. In this demo app, we will upload a photo after selecting from the Gallery. Hope it helps!

build.gradle file:

dependencies {
...
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
...
}

WebAPIService.java file:

public interface WebAPIService {

@Multipart
@POST("/api/fileupload")
Call<ResponseBody> postFile(@Part MultipartBody.Part file, @Part("description") RequestBody description);
}

FileActivity.java file:

...
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class FileActivity extends AppCompatActivity {

private final Context mContext = this;
private final String API_URL_BASE = "http://serverip:port";
private final String LOG_TAG = "BNK";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);

selectImage(); // selects a photo from Gallery
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 100) {
Uri fileUri = data.getData();
if (fileUri != null) {
uploadFile(fileUri); // uploads the file to the web service
}
}
}

private void uploadFile(Uri fileUri) {

String filePath = getRealPathFromUri(fileUri);
if (filePath != null && !filePath.isEmpty()) {
File file = new File(filePath);
if (file.exists()) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL_BASE)
.build();

WebAPIService service = retrofit.create(WebAPIService.class);

// creates RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual filename
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// adds another part within the multipart request
String descriptionString = "Sample description";
RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
// executes the request
Call<ResponseBody> call = service.postFile(body, description);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.i(LOG_TAG, "success");
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(LOG_TAG, t.getMessage());
}
});
}
}
}

private void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 100);
}

public String getRealPathFromUri(final Uri uri) {
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(mContext, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

return getDataColumn(mContext, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}

final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};

return getDataColumn(mContext, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

return getDataColumn(mContext, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

private String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {

Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};

try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

private boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

private boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

private boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}

private boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}

Tomcat : Required request part 'file' is not present

Put this code into config file

@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(-1);
return multipartResolver;
}

POST Multipart Form Data using Retrofit 2.0 including image

I am highlighting the solution in both 1.9 and 2.0 since it is useful for some

In 1.9, I think the better solution is to save the file to disk and use it as Typed file like:

RetroFit 1.9

(I don't know about your server-side implementation) have an API interface method similar to this

@POST("/en/Api/Results/UploadFile")
void UploadFile(@Part("file") TypedFile file,
@Part("folder") String folder,
Callback<Response> callback);

And use it like

TypedFile file = new TypedFile("multipart/form-data",
new File(path));

For RetroFit 2 Use the following method

RetroFit 2.0 ( This was a workaround for an issue in RetroFit 2 which is fixed now, for the correct method refer jimmy0251's answer)

API Interface:

public interface ApiInterface {

@Multipart
@POST("/api/Accounts/editaccount")
Call<User> editUser(@Header("Authorization") String authorization,
@Part("file\"; filename=\"pp.png\" ") RequestBody file,
@Part("FirstName") RequestBody fname,
@Part("Id") RequestBody id);
}

Use it like:

File file = new File(imageUri.getPath());

RequestBody fbody = RequestBody.create(MediaType.parse("image/*"),
file);

RequestBody name = RequestBody.create(MediaType.parse("text/plain"),
firstNameField.getText()
.toString());

RequestBody id = RequestBody.create(MediaType.parse("text/plain"),
AZUtils.getUserId(this));

Call<User> call = client.editUser(AZUtils.getToken(this),
fbody,
name,
id);

call.enqueue(new Callback<User>() {

@Override
public void onResponse(retrofit.Response<User> response,
Retrofit retrofit) {

AZUtils.printObject(response.body());
}

@Override
public void onFailure(Throwable t) {

t.printStackTrace();
}
});

Multipart Request

Almost had it, the save method signature should be:

@Multipart
@POST(URI)
void save(@Part("event") Event event,
Callback<Resource<Event>> callback);

Spring File Upload - 'Required request part is not present'

I finally solved the issue and sharing my solution in case someone else may face the same problem.

@RestController
@RequestMapping("/")
public class ConfigurationController {

@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}

@Bean
public MultipartResolver multipartResolver() {
org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
@PostMapping(value = "/config", consumes = "multipart/form-data")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("password") String password, @RequestParam("file") MultipartFile submissions)
throws AdminAuthenticationException, ConfigurationException {
return ResponseEntity.ok().body(null);
}
}

Retrofit post a object parameter, but receive a parameter is not present error

From Android (or whatever the client it maybe) you're posting JSON body. In order to receive that JSON body parameter in server you've to use Spring Boot @RequestBody() annotation instead of @RequestParam() annotation.

So, in your Spring Boot API code just change the annotation from @RequestParam() to @RequestBody(). That's it!

@PostMapping("create")
fun saveMedias(
@RequestHeader("token") token: String,
/* notice the change here ==> */ @RequestBody mediaGroup: WalkMediaGroup):Result<String> {
}

iOS when send json file to server, it shows required MultipartFile parameter 'file' is not present

I have solved problem by using the following codes:

ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:dataUrl]; 
[request setDelegate:self]; [request setFile:foofile forKey:@"file"];
[request startAsynchronous];

Using ASIHttpRequest could save a lot time, I think.



Related Topics



Leave a reply



Submit