I'm trying to retrieve file(.dat) with size of 1 GB, but, after retrieving some bytes from server it shows no connection of Ftp Server but, this program throws no error and hangs, my code runs perfactly fine for small file but, for big file it fails
code is as follows
//Ftp Connection
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//File Downloading
File downloadFile = new File(savePath);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
isFileDownloaded= ftpClient.retrieveFile(remoteFilePath, outputStream);`
Related
I want to develop a java program to download a file from SFTP server to remote server. That remote server doesn't have any shared path. I have to directly download from sftp and paste in remote windows server driver (D:).
code:
int ftpPort = 22;
JSch jsch = new JSch ();
Session session = null;
Channel channel = null;
ChannelSftp c = null;
try {
session = jsch.getSession(username, hostname, ftpPort);
logger.info("*** FTP Session created. ***");
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
logger.info("*** Session connected. ***");
//Open the SFTP channel
logger.info("*** Opening FTP Channel. ***");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
//Change to the remote directory
logger.info("*** Changing to FTP remote dir: " + remoteDirectory + " ***");
c.cd(remoteDirectory);
//Send the file we generated
try {
logger.info("*** Storing file:'" + filename + "' to local directory: '"+localDirectory+"'");
I am using Jsch and ChannelSftp for connecting to SFTP server.
As of now the above code downloads the code to local path and shared path.
Any suggestion to download the files to Remote server(windows) which doesn't hav any shared path.
Thanks.
Your code would need to run on the remote server directly and download the files from SFTP to its local disk.
Another way would be to download the file on your local machine using your code and then use something like SCP to transfer the file on the remote server, if you really don't have any shared folders.
scp /path/to/your/file user#host:/remote/path
But you said, it's Windows, so you might need to set up SSH/SCP first on that machine.
I create three virtual machines. I used with one master, one slave and client node. And then I wrote java program in client machine.This program is used for writing file from client local file system to master hdfs.
I used hadoop is my client host name and server1 is my namenode host name
Here is my code:
Configuration configuration=new Configuration();
InputStream inputStream =new BufferedInputStream(new FileInputStream("HadoopFile.txt"));
FileSystem hdfs = FileSystem.get(new URI("hdfs://server1:9000"),configuration);
OutputStream outputStream = hdfs.create(new Path(hdfs://server1:9000/home/hadoop/HadoopF.txt));
try { IOUtils.copyBytes(inputStream,outputStream,4096,false); }
After I run this program, I encounter error like:
Call from hadoop/127.0.0.1 to server1:9000 failed to connection exception
I am trying to download a .torrent file from a URL using Java NIO. Downloading this file from a web browser results in a proper torrent file which can be used. Downloading in Java results in a different/corrupted file.
This is the code I am using to perform the download:
URL url = new URL("http://torcache.net/torrent/E4B5326AA31D1AE0C30E05129193E87AA086F5F8.torrent");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream("test.torrent");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
I'm trying to transfer a pgp file with apache.commons.net.ftp.FTPClient, result seems successfully, but when I want to convert it to a txt file I encounter to this error:
gpg: [don't know]: invalid packet (ctb=20)
and when I check the exact size of downloaded file, I notice that it's size is about 1KB less than original file.
here is the code for downloading file:
FileOutputStream fos = new FileOutputStream(Localfilename);
InputStream inputStream = ftpClient.retrieveFileStream(remoteFileDir);
IOUtils.copy(inputStream, fos);
fos.flush();
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(inputStream);
boolean commandOK = ftpClient.completePendingCommand();
can any one understand what is mistake with my way or code?
[edited] noted that the original file decode (convert to txt)successfully, so the problem occures while downloading file.
[edited2] I run the program in my windows desktop and download file in windows, no problem for decode, and I understand that when I run my program with linux server this problem appears!
I found my problem!
The problem was with addressing the remote path, a silly mistake!
so If any one has this problem recheck and recheck again the addresses.
String dirPath = "\\\\tent\\AAA\\Apps\\DCS\\DCMS\\PASE2E\\PAS\\Home\\ArchivePDF";
FTPClient ftpClient = new FTPClient();
ftpClient.connect("192.168.20.143");
ftpClient.login("ggh2ban", "password");
ftpClient.setRemoteVerificationEnabled(false);
ftpClient.enterLocalPassiveMode();
FTPFile[] files = ftpClient.listFiles(dirPath);
for (FTPFile file : files) {
String details = file.getName();
if(file.isFile())
{
System.out.println("Name is "+details);
}
This is my code to read files from ArchivePDF folder. its getting the following Error. Anyone with any ideas?
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
at com.file.FTPFileOps.main(FTPFileOps.java:20)
FTPFileOps.java:20= the IP-Address Line! I am accesing my Client machine via Remote Desk Conn. and the file is in the shared Network, address of which is mentioned above.
Thanks!
Windows shares don't use the FTP protocol but the SMB protocol.
On a Windows machine, to connect using the current user, you may just open the file using java.io.File:
new File("\\\\tent\\AAA\\Apps\\DCS\\DCMS\\PASE2E\\PAS\\Home\\ArchivePDF");
If you want to specify another username/password or run on a non windows machine, you can use the JCIFS library (http://jcifs.samba.org/) and do something like:
SmbFile file = new SmbFile("smb://ggh2ban:password#192.168.20.143/tent/AAA/Apps/DCS/DCMS/PASE2E/PAS/Home/ArchivePDF");
And then use the JCIFS APIs to navigate on the remote file system.