最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询
/**
* 将文件上传到服务器
*
* @param filePath
* 文件路径
* @param channelSftp
* channelSftp对象
* @return
*/
public static boolean uploadFile(String filePath, ChannelSftp channelSftp) {
OutputStream outstream = null;
InputStream instream = null;
boolean successFlag = false;
try {
File isfile = new File(filePath);
if (isfile.isFile()) {
outstream = channelSftp.put(isfile.getName());
File file = new File(filePath);
if (file.exists()) {
instream = new FileInputStream(file);
byte b[] = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
}
successFlag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (instream != null) {
instream.close();
}
if (outstream != null) {
outstream.close();
}
} catch (IOException e) {
e.printStackTrace();<