Question : Secure FTP in Java

Hi Expert

I have a method that uploads files to a FTP site. But i am struggling a bit to does some things.

Well I want to upload max 5 files at once (*.xml) and receive a message that upload the same files again with a different ext (*.eof)

E.g.
12.xml
34.xml
56.xml
78.xml
910.xml

After this take the same files name with a different ext
12.eof
34.eof
56.eof
78.eof
910.eof

Then move then to a other folder
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
package fileuploads;

import com.jcraft.jsch.*;
import java.io.*;
import java.util.*;

public class TestJSch {

	public TestJSch() {

	}

	public static void main(String[] args) {

		try {
			String ftpHost = "ipaddress";
			int ftpPort = port;
			String ftpUserName = "username";
			String ftpPassword = "password";
			String ftpRemoteDirectory = "//dropzone/inbound/moneymarket/mmconfirmations/dev";
			String fileToTransmit  = "C:/tmp/*.txt";

			// First Create a JSch session

			System.out.println("Creating session.");
			JSch jsch = new JSch();
			Session session = null;
			Channel channel = null;
			ChannelSftp c = null;

			// Now connect and SFTP to the SFTP Server
			//
			try {
				// Create a session sending through our username and password
				session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
				System.out.println("Session created.");
				session.setPassword(ftpPassword);

				// Setup Strict HostKeyChecking to no so we dont get the unknown
				// host key exception
				java.util.Properties config = new java.util.Properties();
				config.put("StrictHostKeyChecking", "no");
				session.setConfig(config);
				session.connect();
				System.out.println("Session connected.");

				// Open the SFTP channel
				//
				System.out.println("Opening Channel.");
				channel = session.openChannel("sftp");
				channel.connect();
				c = (ChannelSftp) channel;
			} catch (Exception e) {
				System.err.println("Unable to connect to FTP server."
						+ e.toString());
				throw e;
			}

			// Change to the remote directory
			//
			System.out.println("Changing to FTP remote dir: "
					+ ftpRemoteDirectory);
			c.cd(ftpRemoteDirectory);

			// Send the file we generated
			try {
				File f = new File(fileToTransmit);
				System.out.println("Storing file as remote filename: "	+ f.getName());
				c.put(new FileInputStream(f), f.getName());
			} catch (Exception e) {
				System.err.println("Storing remote file failed." + e.toString());
				throw e;
			}

			// Get the list of files in the remote server directory

			Vector files = c.ls(ftpRemoteDirectory);
			// Log if we have nothing to download
			if (files.size() == 0) {
				System.out.println("No files are available for	download.");
			}


		}
			catch (Exception e) {
				System.err.println("Error: " + e.toString());
		}

		System.out.println("Process Complete.");
		System.exit(0);

	}
}

Answer : Secure FTP in Java

Replace the listFiles () method with the implementation below. You have again given the extension in the ensWith() method wrong; it cannot be *.xml it would have to be the extension that you pass to the method.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public static File[] listFiles(String directory, final String extension){
	    	
	    	File dir = new File(directory);
	    	
	    	FileFilter fileFilter = new FileFilter() { 
	    		public boolean accept(File file) { 
	    			return file.getName().endsWith(extension); 
	    		} 
	    	};
	    	return dir.listFiles(fileFilter); 
	    }
Random Solutions  
 
programming4us programming4us