Spring FileCopyUtils With Strange Behavior

3

I do not know if anyone here uses the FileCopyUtils offered by Spring , but I'm using an image replication method on my system. It aims to "get" the images that are in the temp folder of my system and replicate to another connect 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, problem people is that at the time of checking if my base path exists on the machine and copying the files it shoots the exeption FileNotFoundException . But funny that in my machine behavior is normal. now when this exception is thrown on the server and fired.

I would like to know if you agree with my logic and if I am wrong about something.

Hugs!

EDITING:

Below is the output of 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)
    
asked by anonymous 27.08.2014 / 00:25

1 answer

3

Try recursively creating Path for the directory that will work:

Change where the base.mkdirs () is by:

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

Recursive method to create 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();
}
    
27.08.2014 / 00:55