java不用jsch实现sftp远程下载
时间: 2024-02-06 10:04:21 浏览: 31
以下是使用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 remoteFilePath = "sftp://example.com/path/to/remote/file.txt";
String localFilePath = "/path/to/local/file.txt";
try {
URL url = new URL(remoteFilePath);
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();
}
}
}
```
这段代码使用Java的URL和URLConnection类来建立与远程SFTP服务器的连接,并通过输入流将远程文件的内容读取到本地文件中。你只需要将`remoteFilePath`和`localFilePath`替换为实际的远程和本地文件路径即可。