java不用jsch实现sftp下载
时间: 2024-01-18 10:18:08 浏览: 113
以下是使用Java实现SFTP下载的示例代码,不需要使用JSch库:
```java
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class SFTPDownloader {
public static void main(String[] args) {
String sftpUrl = "sftp://username:password@hostname/path/to/file";
String localFilePath = "/path/to/local/file";
try {
URL url = new URL(sftpUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(localFilePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,上述代码中的`sftpUrl`需要替换为实际的SFTP服务器地址、用户名、密码和文件路径,`localFilePath`需要替换为要保存到本地的文件路径。
阅读全文
相关推荐


















