Error 415 while trying to @POST image with MULTIPART_FORM_DATA (Retrofit)

Viewed 200

I have an android project in which I'm sending an image to my Intellij server just to save it in a folder. This is the following code in Android:

EscanerFragment.java

upload.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("CheckResult")
            @Override
            public void onClick(View v) {

                File file = imgToFile(img);
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part myPic = MultipartBody.Part.createFormData("imagen", file.getName(), requestFile);

                Single.fromCallable(() -> daoParte.subirScan(myPic))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(subirScan -> {
                            subirScan.peek(subido -> {
                                Log.d(DEBUG_TAG, "subirScan:success");
                            }).peekLeft(error -> {
                                Log.d(DEBUG_TAG, "subirScan:failure");
                                Log.d(DEBUG_TAG, error.getMessage());
                                Toast.makeText(view.getContext(), "Error al subir el parte escaneado",
                                        Toast.LENGTH_LONG).show();
                            });
                        });
            }
        });

That code snippet runs when I click on a certain button. This next one is the code for the file writting of that ImageView:

private File imgToFile(ImageView img) {
        BitmapDrawable draw = (BitmapDrawable) img.getDrawable();
        Bitmap bitmap = draw.getBitmap();
        try {
            FileOutputStream outStream = null;
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/Pictures");
            dir.mkdirs();
            String fileName = String.format("%d.jpg", System.currentTimeMillis());
            File outFile = new File(dir, fileName);
            outStream = new FileOutputStream(outFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
            return outFile;
        } catch (Exception e) {
            e.getStackTrace();
            return null;
        }
    }

And these next ones are the DAO and API calls:

DaoParte.java

public Either<ApiError, Boolean> subirScan(MultipartBody.Part img) {
        try {
            Call<Boolean> call = parteAPI.subirScan(img);
            Response<Boolean> response = call.execute();
            if (response.isSuccessful()) {
                if (response.body() != null) {
                    return Either.right(response.body());
                } else {
                    return Either.left(ApiError.builder().message("Error al enviar el documento escaneado").fecha(LocalDateTime.now()).build());
                }
            } else {
                return Either.left(ApiError.builder().message("Error de comunicacion").fecha(LocalDateTime.now()).build());
            }
        } catch (Exception e) {
            Log.e(DEBUG_TAG, e.getMessage(), e);
            return Either.left(ApiError.builder().message("Error de comunicacion").fecha(LocalDateTime.now()).build());
        }
    }

ParteAPI

@Multipart
    @POST("api/partes/subirScan")
    Call<Boolean> subirScan(@Part MultipartBody.Part img);

When I try to retrieve my image in the server I get error 415 or this error

[[FATAL] No injection source found for a parameter of type public java.lang.Boolean EE.rest.RestPartes.subirScan(org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/json], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class EE.rest.RestPartes, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@27b9fb3c]}, definitionMethod=public java.lang.Boolean EE.rest.RestPartes.subirScan(org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=image, defaultValue=null]], responseType=class java.lang.Boolean}, nameBindings=[]}']. Please see server.log for more details."

RestPartes.java

package EE.rest;

import dao.modelo.Parte;
import okhttp3.MultipartBody;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import servicios.ServiciosPartes;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("partes")
public class RestPartes {

    private ServiciosPartes sp = new ServiciosPartes();

    @POST
    @Path("/sancionar")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Parte sancionar(@Body String[] datos, @Context HttpServletRequest request) {
        return sp.sancionar(datos);
    }

    @GET
    @Path("/getAll")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public List<Parte> getPartes() {
        return sp.getPartes();
    }

    @POST
    @Path("/subirScan")
    @Headers("Content-Type:multipart/form-data")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void subirScan(FormDataMultiPart multiPart) {
        System.out.println("a");
    }
}

What can I do? I've basically tried everything I've seen.

Disclaimer: this is my first question, let me know if you need more details about my problem.

0 Answers
Related