File Upload using SFTP - java

I'm trying to upload a file to webapps folder of tomcat in my local system through the following code, also do we require any installations to be done before using this code
public class DeployManager {
public static void main(String[] args) {
String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\webapps\\";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("..");
channelSftp.cd(SFTPWORKINGDIR);
File f = new File("C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");
channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
ex.printStackTrace();
}
}}
but I'm getting the following error:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at com.autohost.java.DeployManager.main(DeployManager.java:30)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcraft.jsch.Util.createSocket(Util.java:252)
... 3 more

You need to make sure sftp running on the machine with IP "10.74.64.102" . I guess its your local windows machine .

Related

Multiple file downloads from SFTP using JSch failing with "Request queue: Unknown request issue"

I am trying to download multiple files from FTP server. It works fine when i simply put it through a for loop . I don't know the file size which may vary , might be too small or too big. So, i am downloading them using executor service. When i put the download function through executor, i get Request queue: Unknown request issue . Please let me know where i'm going wrong.
System.out.println("Connecting to FTP Server");
Session session = null;
ChannelSftp channelSftp = null;
JSch jsch = new JSch();
session = jsch.getSession(sftpUser,sftpHost,22);
session.setPassword(sftpPassword);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Session Connected Status: "+session.isConnected());
Channel channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
System.out.println("Channel Connected Status: "+channelSftp.isConnected());
System.out.println("File: "+sourceFilePath);
if(channelSftp.isConnected())
{
String path = this.propertyManager.getValue("targetDirectoryPath");
channelSftp.cd(path);
Vector filelist = channelSftp.ls(path);
for(int i=0; i<filelist.size();i++)
{
LsEntry entry = (LsEntry) filelist.get(i);
String fileName = sourceFilePath + entry.getFilename();
destinationFilePath = ShipmentUtils.getDownloadPath(entry.getFilename());
System.err.println(entry.getFilename());
exec.execute(new FileDownloader(destinationFilePath, channelSftp.get(fileName)));
}
}
channelSftp.disconnect();
session.disconnect();
Here is the downloader class
private class FileDownloader implements Runnable
{
String destinationFilePath;
InputStream stream;
public FileDownloader(String destinationFilePath , InputStream stream) {
this.destinationFilePath = destinationFilePath;
this.stream = stream;
}
#Override
public void run() {
byte[] buffer = new byte[1024];
BufferedOutputStream bos = null;
BufferedInputStream bis = null ;
try
{
bis = new BufferedInputStream(stream);
File newFile = new File(destinationFilePath);
OutputStream os = new FileOutputStream(newFile);
bos = new BufferedOutputStream(os);
int readCount=0;
while( (readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(bis != null) bis.close();
if(bos != null) bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This is the error
java.io.IOException: error: 4: RequestQueue: unknown request id 1399811183
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1406)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at com.shipmentprocessing.network.FTPConnector$FileDownloader.run(FTPConnector.java:141)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.io.IOException: error
at com.jcraft.jsch.ChannelSftp$2.close(ChannelSftp.java:1505)
at java.io.BufferedInputStream.close(Unknown Source)
at com.shipmentprocessing.network.FTPConnector$FileDownloader.run(FTPConnector.java:150)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
While you can use one SFTP connection/JSch session for multiple downloads, the JSch session is definitely not thread safe.
So if you need to run the downloads is separate threads, you have to open new connection for (in) each thread. Your download performance will be much better this way too.

Connecting to remote Windows machine with JSch

I want to execute command on remote system and want to get result of the same.
I tried following code:
package rough;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class RemoteSSH {
public static void main(String[] args) {
String host = "\\\\10.209.110.114";
String user = "Administrator";
String password = "Admin123";
String command1 = "ipconfig";
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
But I am getting following error:
com.jcraft.jsch.JSchException: java.lang.IllegalArgumentException: protocol = socket host = null
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.lang.IllegalArgumentException: protocol = socket host = null
at sun.net.spi.DefaultProxySelector.select(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcraft.jsch.Util.createSocket(Util.java:252)
... 3 more
Any solution for it?
I have already tried with pstool also but not getting correct output.
After removing the \\\\ from the hostname, I'm getting:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
My wild guess is that you have no SSH server running on the target machine.
Note that Windows does not have any built-in SSH server.
Make sure you install some SSH server first.
See Is IIS SFTP natively supported by Windows Server 2012 R2?
Or use a different technology to run the command:
Best way to run remote commands on a Windows server from Java?

Connect to Localhost with Java FTP

I'm developing FTP program on JAVA. I'm using Apache Commons Net Library. My Codes are below.
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class ServerClass {
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
public static void main(String[] args) {
String server = "127.0.0.1";
int port = 80;
String user = "root";
String pass = "root";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
showServerReply(ftpClient);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("Operation failed. Server reply code: " + replyCode);
return;
}
boolean success = ftpClient.login(user, pass);
showServerReply(ftpClient);
if (!success) {
System.out.println("Could not login to the server");
return;
} else {
System.out.println("LOGGED IN SERVER");
}
} catch (IOException ex) {
System.out.println("Oops! Something wrong happened");
ex.printStackTrace();
}
}
}
But I can't connect my localhost. I want to login my localhost and see my file.
My errors are below.
Oops! Something wrong happened
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
at com.emrecanoztas.ftp.ServerClass.main(ServerClass.java:22)
can you help me anybody? Thanks!..
Questions:
Is there an FTP server running?
Is there a message in the FTP server log about the connect request?
Does the FTP server allow connections from localhost?
Is the FTP server listening on localhost or should you use the public IP/name of the computer? (check with netstat)
Try the settings below.
String server = "ftp.icm.edu.pl";
int port = 21;
String user = "anonymous";
String pass = "me#nowhere.com";

Getting connection errors while making a server/client chat program in java

I'm nowhere near done with this project yet I'm just trying to make sure things are working and more specifically that the client and server are connecting, and I'm getting connection errors. Here are my two programs. I'm also not entirely sure how to run both of them at once in eclipse so maybe that is the problem. Any help is much appreciated
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
public void main(String[] args)throws Exception {
ServerSocket server = new ServerSocket(3333);;
while (true)
{
Socket clientsocket = server.accept();
Scanner sc = new Scanner(clientsocket.getInputStream());
PrintStream p = new PrintStream(clientsocket.getOutputStream());
String message = "Hello from server";
p.print(message);
}
}
And heres the client
public static void main(String[] args)throws Exception {
Client display = new Client();
display.setVisible(true);
try {
Socket server = new Socket("localhost", 3333);
Scanner sc = new Scanner(server.getInputStream());
PrintStream p = new PrintStream(server.getOutputStream());
System.out.println(sc.next());
server.close();
sc.close();
p.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have some gui stuff above and below the client that I didnt show. When I run these I get the following errors
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:56)
and line 56 is the line with -- Socket server = new Socket("localhost", 3333);

java.net.UnknownHostException not able to connect to ftp

I have ftp port as: ftp://173.201.0.1/
I am trying to connect it through following:
String Ftp_Path = "ftp://173.201.0.1/";
public List<String> GetFileList()
{
String ftpServerIP = Ftp_Path;
String ftpUserID = Ftp_UserName;
String ftpPassword = Ftp_Password;
FTPFile[] downloadFiles = null;
StringBuilder result = new StringBuilder();
FTPClient ftp = new FTPClient();
List<String> xlsFiles = null;
try {
ftp.connect(Ftp_Path);
ftp.login(ftpUserID, ftpPassword);
downloadFiles=ftp.listFiles();
xlsFiles = new ArrayList<String>();
for(FTPFile i : downloadFiles) {
if(i.toString().endsWith(".xls")) {
xlsFiles.add(i.toString());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xlsFiles;
}
But I am getting error on line:
ftp.connect(Ftp_Path);
Following is the error.
java.net.UnknownHostException: ftp://173.201.0.1/
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.apache.commons.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:92)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:201)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:289)
at com.amazonaws.mws.samples.ImportRulesPropertyClass.GetFileList(ImportRulesPropertyClass.java:33)
at com.amazonaws.mws.samples.ManageReportScheduleSample.main(ManageReportScheduleSample.java:74)
Plase help me.
I am new with java.
You just need to specify the IP.The FTPClient makes a ftp request.It is not similar to http request.Just change
String Ftp_Path = "ftp://173.201.0.1/";
to
String Ftp_Path = "173.201.0.1";
Also check whether the ftp port is up and accessible through telnet

Categories

Resources