java FTP 实现远程上传下载功能

Mr_XK 发布于 2014/12/12 14:55
阅读 3K+
收藏 0

 最近使用java FtpClient 实现远程上传下载功能代码如下

public long downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {
        long count=0;
        try {
            if (connectServer(ip, port, username, password)) {
                if (remotePath.length() != 0) {
                    ftpClient.cd(remotePath);
                }
                ftpClient.binary();
                TelnetInputStream is = ftpClient.get(remoteFileName);
                File fp = new File(localPath);
                if (!fp.exists()) {
                    fp.mkdirs();
                }
                File file_out = new File(localPath + File.separator + localFileName);
                FileOutputStream os = new FileOutputStream(file_out);
                byte[] bytes = new byte[1024];
                int readBye;
                while ((readBye = is.read(bytes)) != -1) {
                    os.write(bytes, 0, readBye);
                    count+=readBye;
                }
                is.close();
                os.close();
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            count=0;
        }
        return count;
    }

再执行到红色代码区报| Error java.io.FileNotFoundException: CWD /home/ftp: 550 Failed to change directory.

| Error     at sun.net.ftp.FtpClient.readReply(FtpClient.java:236)
| Error     at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:193)
| Error     at sun.net.ftp.FtpClient.issueCommandCheck(FtpClient.java:206)
| Error     at sun.net.ftp.FtpClient.cd(FtpClient.java:658)
| Error     at com.pactera.util.FtpUtil.downloadFile(FtpUtil.java:106)
| Error     at com.pactera.util.FtpUtil$downloadFile.call(Unknown Source)
| Error     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
| Error     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
| Error     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
| Error     at com.pactera.grade.GradeController.handleExportButton(GradeController.groovy:289)
| Error     at com.pactera.grade.GradeController.handleExportButton(GradeController.groovy)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
| Error     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
| Error     at java.lang.reflect.Method.invoke(Method.java:597)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:69)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:340)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:223)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:189)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:116)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72)
| Error     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
| Error     at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:328)
| Error     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
| Error     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
| Error     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
| Error     at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
| Error     at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
| Error     at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
| Error     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
| Error     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
| Error     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
| Error     at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:314)
| Error     at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:279)
| Error     at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:270)
| Error     at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:222)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:206)
| Error     at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:152)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at javax.servlet.FilterChain$doFilter.call(Unknown Source)
| Error     at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:44)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:66)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
| Error     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
| Error     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
| Error     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
| Error     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
| Error     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
| Error     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
| Error     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
| Error     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
| Error     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
| Error     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
| Error     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
| Error     at java.lang.Thread.run(Thread.java:619)

在执行红色代码区报的异常,我的FTP服务器上本身存在那个路径啊,再命令窗口中也上传过啊,都好使,但是执行代码就不好使了。调用如下方法,FTP服务器是LINUX 的.

client.downloadFile("/home/weblogic/excel/agreement", uploadPath, fileName, fileName);



加载中
返回顶部
顶部