原标题: 你可以使用Java的URL类和URLConnection类来下载文件到本地指定文件夹。以下是一个简单的示例:
导读:
```javaimport java.io.*;import java.net.URL;import java.net.URLConnection;public class F...
```java
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class FileDownloader {
public static void main(String[] args) {
String fileUrl = "-to-download.txt";
String saveDir = "/path/to/save/directory/";
try {
URL url = new URL(fileUrl);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
// 获取文件名
String fileName = getFileName(fileUrl);
// 在保存目录中创建文件
File outputFile = new File(saveDir + "/" + fileName);
FileOutputStream outputStream=
new FileOutputStream(outputFile);
byte[] buffer=new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
inputStream.close();
outputStream.close();
System.out.println("Downloaded successfully!");
} catch (IOException e) {
e.printStackTrace();