Openshift - java.net.BindException:权限被拒绝

时间:2015-09-16 02:35:08

标签: java ftp openshift apache-commons

我正在尝试创建一个简单的java应用程序,它从URL下载文件,存储文件,然后将文件上传到FTP以存储它。

我有一个(本地)工作代码,可以将文件下载到C:\上的本地计算机上,并使用本地文件上传到FTP。

我想将此应用程序移动到OpenShift并从那里在Tomcat6中运行它。这意味着我必须将C:\驱动器引用更改为OpenShift中的目录。我引用了“tmp”目录。

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.log4j.Logger;


    public class AppRun {


        public static void main(String[] args) {

            Logger log = Logger.getLogger(AppRun.class);

            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            String formattedDate = sdf.format(date);

            downloadFile(formattedDate);
            uploadFile(formattedDate);
        }

        // FILE DOWNLOAD
        public static void downloadFile(String formattedDate){

            Logger log = Logger.getLogger(AppRun.class);

            String sourceURL = "http://websiteURL/pdf/"+formattedDate+".pdf";
            String destinationLocation = "/tmp/"+formattedDate+".pdf";
            try {
                org.apache.commons.io.FileUtils.copyURLToFile(
                                new URL(sourceURL), 
                                new File(destinationLocation)
                                );
                log.warn("/tmp/"+formattedDate+".pdf downloaded successfully!" );
            } 
            catch (Exception e) { 
                log.error(e);
                log.error("Source URL : " + "http://websiteURL/pdf/"+formattedDate+".pdf" + " Destination URL : " + "/tmp/"+formattedDate+".pdf");
                System.out.println("No file found!");
            }

        }

        // FILE UPLOAD
        public static void uploadFile(String formattedDate){

            Logger log = Logger.getLogger(AppRun.class);

            FTPClient client = new FTPClient();
            FileInputStream fis = null;

            try {

                client.connect("ftp.domain.com");
                client.login("user", "pass");

                // Create an InputStream of the file to be uploaded         
                String originalFile = "/tmp/"+formattedDate+".pdf";
                fis = new FileInputStream(originalFile);

                //
                // Store file to server
                //
                String destinationFileName = formattedDate +".pdf";
                client.storeFile(destinationFileName, fis);
                log.warn("File " + formattedDate +".pdf uploaded successfully!");
                client.logout();

            } catch (IOException e) {
                log.error(e);
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    client.disconnect();
                } catch (IOException e) {
                    log.error(e);
                    e.printStackTrace();
                }
            }

        }
    }

当我运行代码时,我可以下载文件,但上传部分失败了:

  。

的/ var / lib中/ openshift / XXXXXXXXXXXXXXXXXXXX / APP-根/运行/回购// openshift / cron的/微小/ JAVA:   2015-09-15 22:27:08 WARN AppRun:41 - /tmp/20150915.pdf已下载   成功! 2015-09-15 22:27:09错误AppRun:77 -   java.net.BindException:权限被拒绝java.net.BindException:   没有权限           at java.net.PlainSocketImpl.socketBind(Native Method)           在java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)           在java.net.ServerSocket.bind(ServerSocket.java:376)           在java.net.ServerSocket。(ServerSocket.java:237)           在javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:231)           在org.apache.commons.net.ftp.FTPClient。 openDataConnection (FTPClient.java:797)           在org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633)           在org.apache.commons.net.ftp.FTPClient .__ storeFile(FTPClient.java:624)           在org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)           在AppRun.uploadFile(AppRun.java:72)           在AppRun.main(AppRun.java:25)

任何建议如何解决这个问题将不胜感激!感谢。

1 个答案:

答案 0 :(得分:0)

为FTP PASSIVE模式配置客户端。

登录后尝试添加ftp.enterLocalPassiveMode()