Filecopyutils of Spring With strange behavior

Asked

Viewed 71 times

3

I don’t know if anyone here uses the Filecopyutils offered by Spring, but I’m using it in a imaging replication method in my system. It aims to "grab" the images that are in my system’s temp folder and replicate to another link on the server. Below is the method with some comments:

public Set<Image> imageReplication(String destination) throws IOException {
    Set<Image> img = new HashSet<>();
    Image image =null;
    try {
        List<ImageJson> imagePathTmpList = dashboardFacade.getImagePathTmpList();

        Gson gson = null;

        String subdomain = dashboardFacade.getAgency().getSubdomain();
        File base = new File("/home/manolo/joocebox-img/" + subdomain + "/"+ destination);


        for (ImageJson imageJsonList : imagePathTmpList) {
            gson = new Gson();
            image = new Image();

            FileMeta imgTmp = gson.fromJson(imageJsonList.getJson(), FileMeta.class);
            //Caminho antigo do arquivo
            String oldPath = imgTmp.getFileTmpPath();

                        //Novo caminho concatenando o nome do arquivo.
            String newPath = base + "/" + imgTmp.getFileName();

            //Verifica se o diretorio base exesti, caso não ele cria o caminho e faz a copia dos arquivos. Creio que aqui que dispare a exeption.
            if(!base.exists()){
                base.mkdirs();
                FileCopyUtils.copy(new FileInputStream(oldPath), new FileOutputStream(newPath));
            }else{
                FileCopyUtils.copy(new FileInputStream(oldPath), new FileOutputStream(newPath));
            }

            image.setJson(newPath);
            img.add(image);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }finally{
        deleteTmp();
    }

    return img;
}

Well, personal problem is that at the time of checking whether my base path exists on the machine and in performing the cpia of the files it shoots the exeption Filenotfoundexception. But funny that on my machine the behavior is normal. now when it runs on the server this exception and fired.

I wonder if you agree with my logic and if case I’m wrong about something.

Hug!


EDITING:

Below is the exit of the System.out.println(base.getAbsolutePath()) like the stackTrace:

/home/ubuntu/joocebox-img/manolo/Novo Destino
java.io.FileNotFoundException: /home/ubuntu/joocebox-img/manolo/Novo Destino/FotoMinha.jpg (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
    at br.com.joocebox.controller.FileController.imageReplication(FileController.java:161)
    at br.com.joocebox.controller.FileController$$FastClassBySpringCGLIB$$282cc946.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
    at br.com.joocebox.controller.FileController$$EnhancerBySpringCGLIB$$e30b112a.imageReplication(<generated>)
    at br.com.joocebox.controller.DestinationController.addDestination(DestinationController.java:108)
    at br.com.joocebox.controller.DestinationController$$FastClassBySpringCGLIB$$49da9f7c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
    at br.com.joocebox.controller.DestinationController$$EnhancerBySpringCGLIB$$289546a0.addDestination(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
  • The path this is the one on the server: /home/manolo/joocebox-img/ ? I suggest putting some System.out.println() to see the paths and where the error is.

  • Hi Kyllopardiun, I actually use Amazon so the path is /home/Ubuntu. But joocebox-img doesn’t exist, so I put mkdirs() and right after that make the copy. What intrigues me is that I have already threshed several times here in my local machine and everything seems right to me. Thank you

  • I thought my base.mkdirs(); would check and if the folder does not exist he would soon take responsibility for creating. The strangest that even not having the folder created on my machine mkdirs performs such creation without triggering the exception. Am I making a mistake and I still can’t see?

  • Edit your question and put the whole StackTrace(); and also the value of System.out.println(base.getAbsolutePath()). If you want I can answer with the way to create the mkdir recursivo but I have no guarantee that it will solve your problem.

  • Then @Kyllopardiun would have to deploy the application and run it here. It’s usually a little slow, so if you can tell me how to do the recursive form, I’m very grateful. When I am with the stackTrace in hand I put for you to take a look. Thank you!

  • can be white spaces test without them and check the result.

  • Test string without spaces and the problem still occurs.

Show 2 more comments

1 answer

3


Try creating recursively the Path to the directory that will work:

Trade where the base.mkdirs() is:

    if (!base.exists()) {
        criaPath(base);
    }

Recursive method to create the Path:

/**
 * @param d o diretório a ser criado (recusivamente)
 */
public void criaPath(File d) {
    //verifica se o diretório acima existe (proteger o código contra erros)
    if (!d.getParentFile().exists()) {
        criaPath(d.getParentFile());
    }
    d.mkdir();
}
  • Cool! I will try to use your proposal and inform you. Thank you so much for your help so far :)

  • So Kyllopardium, I implemented the solution as you described above and yet you continue to fire the same exception. It’s totally strange, because my machine works perfectly. Any suggestions? Thanks for your attention.

Browser other questions tagged

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