Error while uploading an AngularJS + Vraptor file

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 did not add any validation error, it is   possible that a conversion 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 did not add any validation error, it is   possible that a conversion had happened. at   br.com.caelum.vraptor.validator.Messages.assertAbsenceOfErrors (Messages.java:108)     at   $ weldClientProxy.assertAbsenceOfErrors (Unknown)   Source) at   br.com.caelum.vraptor.core.DefaultResult.use (DefaultResult.java:79)     at   br.caelum.vraptor.core.DefaultResult $ Proxy $ $$ _ WeldClientProxy.use (Unknown   Source) at   com.union.controller.CORSInterceptor.intercept (CORSInterceptor.java:25)

In that second exception there is an error in my CORS interceptor:

@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 to 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 in front for it to accept files up to 10MB, and that's alright, the problem is that back-end can not get these files.

    
asked by anonymous 14.12.2015 / 12:02

1 answer

3

I was able to solve the problem. It actually was a bit of a lack of attention from me, in the Vraptor documentation says that the default upload value for each file is 2MB so I have to override 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)
    
14.12.2015 / 14:06