0

I want to copy my network side folder to another folder using java. I'm using FileUtils.copyDirectoryToDirectory for this. I wrote this codes in servlet.

    String salesOrder=request.getParameter("salesOrder");       
    String url_archive="http://localhost:8983/solr/archiveCore";
    SolrClient solr_archive=new HttpSolrClient.Builder(url_archive).build();
    ((HttpSolrClient) solr_archive).setParser(new XMLResponseParser());
    SolrQuery query_archive = new SolrQuery();
    query_archive.setQuery("strSO:"+salesOrder);
    query_archive.setRows(999999);
    query_archive.setStart(0);
    query_archive.set("defType", "edismax");
    try {
        QueryResponse resp_archive = solr_archive.query(query_archive);
        SolrDocumentList list_archive = resp_archive.getResults();
        String filePath=list_archive.get(0).getFieldValue("FilePath").toString();
        String[] paths = splitPath(filePath);
        File directoryOriginal=new File("\\\\ptrisf02\\\\group2\\Engine_Follow\\"+paths[1]+"\\"+paths[2]+"\\"+paths[3]);
        File directoryTemp=new File("T:\\Temp\\"+salesOrder);
        if(!directoryTemp.exists())
        {
            directoryTemp.mkdir();
        }
        else
        {
            System.out.println("Folder already created...");
        }
        FileUtils.copyDirectoryToDirectory(directoryOriginal, directoryTemp);
        //copyDirectory(directoryOriginal, directoryTemp);
        System.out.println("Copying completed...");
    } catch (SolrServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static String[] splitPath(String pathString) {
    Path path = Paths.get(pathString);
    return StreamSupport.stream(path.spliterator(), false).map(Path::toString)
                        .toArray(String[]::new);
}

When I'm trying to copy the folder it gives me this error

java.io.IOException: Failed to list contents of \\ptrisf02\group2\Engine_Follow\V2500-A5\V2500-A5_e-Archive\EV12386-03
    at org.apache.commons.io.FileUtils.doCopyDirectory(FileUtils.java:1426)
    at org.apache.commons.io.FileUtils.copyDirectory(FileUtils.java:1388)
    at org.apache.commons.io.FileUtils.copyDirectory(FileUtils.java:1268)
    at org.apache.commons.io.FileUtils.copyDirectoryToDirectory(FileUtils.java:1209)
    at org.solr.copyAllSO.doPost(copyAllSO.java:81)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

So, what is the problem in here? The source folder have a few subfolder is that can make a problem?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
demir5334
  • 215
  • 7
  • 17
  • Does the folder `\\ptrisf02\group2\Engine_Follow\V2500-A5\V2500-A5_e-Archive\EV12386-03` exist? – Joni Aug 18 '20 at 12:19
  • @Joni Yes, it's exist but the folder has user permission. Is that can be a problem? – demir5334 Aug 18 '20 at 12:21
  • 1
    @demir5334 yes, I think it can be. Your error message says it can't list the files from the folder. Can the user performing the copy read in this folder ? – BenjaminD Aug 18 '20 at 14:17

1 Answers1

0

Don’t use FileUtils. It’s old code that uses the ancient java.io.File class, which is why it’s giving you such a vague error.

Copying a directory with the java.nio.file package is easier than you might think:

Path sourceRoot = directoryOriginal.toPath();
Path destRoot = directoryTemp.toPath();

try (Stream<Path> tree = Files.walk(sourceRoot)) {
    Iterator<Path> i = tree.iterator();
    while (i.hasNext()) {
        Path source = i.next();
        Path dest = destRoot.resolve(sourceRoot.relativize(source));
        if (Files.isDirectory(source)) {
            Files.createDirectories(dest);
        } else {
            Files.copy(source, dest);
        }
    }
}

Unlike methods of the File class, java.nio.file.Files methods will either succeed, or will fail with an exception that has a clear message (usually obtained from the underlying operating system) explaining the reason for the failure.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • I tried it and I got this error. **java.nio.file.AccessDeniedException: \\ptrisf02\group2\Engine_Follow\V2500-A5\V2500-A5_e-Archive\EV12386-03** – demir5334 Aug 18 '20 at 16:34
  • @demir5334 And now you know the actual cause of your problem. Either EV12386-03 is not writable or V2500-A5_e-Archive is not writable. – VGR Aug 18 '20 at 16:36
  • Yeah, I know it but I can't give permission on this folder. – demir5334 Aug 18 '20 at 17:39
  • If you don’t have permission to read the folder, there is no way you can copy from it. That is by design. The purpose of such permissions is to prevent unauthorized users and unauthorized programs from accessing the files. – VGR Aug 18 '20 at 19:53
  • You can try Files.copy (https://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java/16600787#16600787) – Woodchuck Aug 18 '20 at 21:54
  • @JWoodchuck I am using Files.copy. Files.copy will copy one file, not an entire directory tree. – VGR Aug 18 '20 at 21:55
  • @JWoodchuck The question is. My answer is not. In fact, that was the point of my answer. – VGR Aug 18 '20 at 22:00