Skip to content

Instantly share code, notes, and snippets.

@madan712
Created June 23, 2015 15:58
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save madan712/810b05f3d2073bad6c16 to your computer and use it in GitHub Desktop.
Save madan712/810b05f3d2073bad6c16 to your computer and use it in GitHub Desktop.
Java program to upload/download files from remote server
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class FTPExample {
private String host;
private Integer port;
private String user;
private String password;
private JSch jsch;
private Session session;
private Channel channel;
private ChannelSftp sftpChannel;
public FTPExample(String host, Integer port, String user, String password) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
}
public void connect() {
System.out.println("connecting..."+host);
try {
jsch = new JSch();
session = jsch.getSession(user, host,port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
}
public void disconnect() {
System.out.println("disconnecting...");
sftpChannel.disconnect();
channel.disconnect();
session.disconnect();
}
public void upload(String fileName, String remoteDir) {
FileInputStream fis = null;
connect();
try {
// Change to output directory
sftpChannel.cd(remoteDir);
// Upload file
File file = new File(fileName);
fis = new FileInputStream(file);
sftpChannel.put(fis, file.getName());
fis.close();
System.out.println("File uploaded successfully - "+ file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
disconnect();
}
public void download(String fileName, String localDir) {
byte[] buffer = new byte[1024];
BufferedInputStream bis;
connect();
try {
// Change to output directory
String cdDir = fileName.substring(0, fileName.lastIndexOf("/") + 1);
sftpChannel.cd(cdDir);
File file = new File(fileName);
bis = new BufferedInputStream(sftpChannel.get(file.getName()));
File newFile = new File(localDir + "/" + file.getName());
// Download file
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();
System.out.println("File downloaded successfully - "+ file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
disconnect();
}
public static void main(String[] args) {
String localPath = "C:/temp/";
String remotePath = "/export/home/madan/";
FTPExample ftp = new FTPExample("10.100.12.14", 2222, "madan", "password");
ftp.upload(localPath+"filetoupload.txt", remotePath);
ftp.download(remotePath+"filetodownload.txt", localPath);
}
}
@vignesh18vijayakumar
Copy link

Can you suggest me the what are all jar files needed in this program.

@kktcoderepository
Copy link

Hi,
I am new to programming & recently started working on an app development using java. Purpose of the app is to process some api & output information in the form of excel file. JAR is located at server and runs when user send request at client(local machine).
The app generates excel file successfully but at the server location. I tried to use the above approach to download from server to my local machine(client location) but it creates a new file at JAR location(i.e at Server location) instead of client location.
The code snippet is as below & this creates a file at JAR location with file name C:\Users\PFName-CFName_Co.xlsx
Please check & help me on how to download the file to client location(anywhere in c drive)

String SerDir=System.getProperty("user.home") + "/Desktop/";
String LocDir="C:\Users\";
String SerFC = SerDir+PFName+"-"+CFName+"_Co.xlsx";
String LocFC = LocDir+PFName+"-"+CFName+"_Cos.xlsx";
Session session = null;
ChannelSftp sftpChannel=null;
Channel channel = null;
JSch jsch = new JSch();
try
{
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking","no");
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

channel = session.openChannel("sftp");
channel.connect();
sftpChannel =(ChannelSftp) channel;
sftpChannel.cd(SerDir); 
File file = new File(SerFC);

byte[] buffer = new byte[1024];
BufferedInputStream bis;
bis = new BufferedInputStream(sftpChannel.get(file.getName()));
File newFile = new File(LocDir + file.getName());
// Download file
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
	bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();

@madan712
Copy link
Author

Hi,
I am new to programming & recently started working on an app development using java. Purpose of the app is to process some api & output information in the form of excel file. JAR is located at server and runs when user send request at client(local machine).
The app generates excel file successfully but at the server location. I tried to use the above approach to download from server to my local machine(client location) but it creates a new file at JAR location(i.e at Server location) instead of client location.
The code snippet is as below & this creates a file at JAR location with file name C:\Users\PFName-CFName_Co.xlsx
Please check & help me on how to download the file to client location(anywhere in c drive)

String SerDir=System.getProperty("user.home") + "/Desktop/";
String LocDir="C:\Users";
String SerFC = SerDir+PFName+"-"+CFName+"_Co.xlsx";
String LocFC = LocDir+PFName+"-"+CFName+"_Cos.xlsx";
Session session = null;
ChannelSftp sftpChannel=null;
Channel channel = null;
JSch jsch = new JSch();
try
{
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking","no");
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

channel = session.openChannel("sftp");
channel.connect();
sftpChannel =(ChannelSftp) channel;
sftpChannel.cd(SerDir); 
File file = new File(SerFC);

byte[] buffer = new byte[1024];
BufferedInputStream bis;
bis = new BufferedInputStream(sftpChannel.get(file.getName()));
File newFile = new File(LocDir + file.getName());
// Download file
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
	bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();

Do u got the solution?
Question - How you are sending the request from client to server. If you know the file path from server in your client code then you can u above download code to retrieve the file.

@kktcoderepository
Copy link

kktcoderepository commented Jun 11, 2019 via email

@manishppp2530
Copy link

Thanks, It worked for me

@ravis2227
Copy link

can you suggest me what are the jar files required for this code

@chirag122
Copy link

How to stream video?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment