Error while uploading an Angularjs+Vraptor file

Asked

Viewed 317 times

5

I have a system where I can upload files through an Angular library (ng-file-upload), but if the file is larger than 3MB when I try to upload I have the following errors:

GRAVE: Servlet.service() for Servlet [default] in context with path [/Union] threw Exception br.com.Caelum.vraptor.Interceptionexception: br.com.Caelum.vraptor.Interceptionexception: java.lang.Illegalstateexception: There are validation errors and you forgot to specify Where to go. Please add in your method Something like: Validator.onErrorUse(page()). of(Anycontroller.class). anyMethod(); or any view that you like. If you didn’t add any validation error, it is possible that a Conversion error had happened. at br.com.Caelum.vraptor.Interceptor.StepInvoker.invokeMethod(Stepinvoker.java:54)

Caused by: java.lang.Illegalstateexception: There are validation errors and you forgot to specify Where to go. Please add in your method Something like: Validator.onErrorUse(page()). of(Anycontroller.class). anyMethod(); or any view that you like. If you didn’t add any validation error, it is possible that a Conversion error had happened. at br.com.Caelum.vraptor.Validator.Messages.assertAbsenceOfErrors(Messages.java:108) at br.com.Caelum.vraptor.Validator.Messages$Proxy$_$$Weldclientproxy.assertAbsenceOfErrors(Unknown Source) at br.com.Caelum.vraptor.core.DefaultResult.use(Defaultresult.java:79) at br.com.Caelum.vraptor.core.Defaultresult$Proxy$$$_Weldclientproxy.use(Unknown Source) at com.union.controller.CORSInterceptor.Intercept(Corsinterceptor.java:25)

In this second Exception gives an error in my Interceptor that treats CORS:

@Intercepts
public class CORSInterceptor {

    @Inject
    private Result result;
    @Inject
    private HttpServletRequest request;

    @BeforeCall
    public void intercept() throws InterceptionException {
        // Fix your origin if possible for security reasons
        String origin = request.getHeader("origin") != null ? request.getHeader("origin") : "*";

        result.use(Results.status()).header("Access-Control-Allow-Origin", origin); //Linha 25
        result.use(Results.status()).header("Access-Control-Allow-Credentials", "true");
        result.use(Results.status()).header("Access-Control-Expose-Headers", "Content-Type, Location");
    }
}

This is my method that receives the front image and saves it in a directory:

@Post
@Path(value = "/imagem")
public void salvaImagem(UploadedFile file) throws IOException {
    System.out.println("SALVAR IMAGEM");
    try{
        InputStream in = new BufferedInputStream(file.getFile());
        File fotoSalva = new File(DIRETORIO_UPLOAD, file.getFileName());
        System.out.println(fotoSalva);
        FileOutputStream fos = new FileOutputStream(fotoSalva);
        while (in.available() != 0) {
            fos.write(in.read());
        }
        fos.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

I’m validating the upload on front for it to accept files up to 10MB, and it’s all right, the same problem is that the back-end can’t get those files.

inserir a descrição da imagem aqui

1 answer

3


I managed to solve the problem. It was actually a bit of a lack of attention on my part, at documentation of Vraptor says that the default upload value for each file is 2MB then I have to overwrite the Upload functions:

@Specializes
@ApplicationScoped
public class CustomMultipartConfig extends DefaultMultipartConfig {

    // alteramos o tamanho total do upload para 50MB
    public long getSizeLimit() {
        return 50 * 1024 * 1024;
    }

    // alteramos o tamanho do upload de cada arquivo para 20MB
    public long getFileSizeLimit() {
        return 20 * 1024 * 1024;
    }
}

Or I can use this annotation, where I set the size to 40mb total and 10mb per file:

@UploadSizeLimit(sizeLimit=40 * 1024 * 1024, fileSizeLimit=10 * 1024 * 1024)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.