简体   繁体   中英

Openshift - java.net.BindException: Permission denied

I am trying to create a simple java application that downloads a file from URL, stores the file and then uploads the file to FTP to store it.

I have a (local) working code that downloads the file to my local machine on C:\\ and uses the local file to upload to FTP.

I would like to move this application to OpenShift and run it in Tomcat6 from there. This means that I have to change the C:\\ drive reference to a directory in OpenShift. I referenced the "tmp" directory.

    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();
                }
            }

        }
    }

When I run the code, I can download the file but the upload part fails:

/var/lib/openshift/XXXXXXXXXXXXXXXXXXXX/app-root/runtime/repo//.openshift/cron/minutely/java: 2015-09-15 22:27:08 WARN AppRun:41 - /tmp/20150915.pdf downloaded successfully! 2015-09-15 22:27:09 ERROR AppRun:77 - java.net.BindException: Permission denied java.net.BindException: Permission denied at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376) at java.net.ServerSocket.bind(ServerSocket.java:376) at java.net.ServerSocket.(ServerSocket.java:237) at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:231) at org.apache.commons.net.ftp.FTPClient. openDataConnection (FTPClient.java:797) at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633) at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624) at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976) at AppRun.uploadFile(AppRun.java:72) at AppRun.main(AppRun.java:25)

Any suggestions how to fix this would be greatly appreciated! Thanks.

Configure the client for FTP PASSIVE mode.

Try adding ftp.enterLocalPassiveMode() after login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM