java实现下载文件流
时间: 2023-11-01 18:10:06 浏览: 34
可以使用Java的URLConnection和InputStream来实现下载文件流,以下是代码示例:
```
URL url = new URL("http://example.com/file.txt");
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("file.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
```
请注意,这仅是示例代码,实际应用中还需要处理错误和异常。
相关问题
java实现下载文件功能
Java实现文件下载功能可以使用Java IO中的InputStream和OutputStream,以及HttpURLConnection类来实现。下面是一个简单的示例代码:
```java
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadFile {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com/file.pdf");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// 判断HTTP响应是否成功
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
// 如果Content-Disposition头部存在,则从中提取文件名
if (disposition != null) {
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
}
// 没有Content-Disposition头部,则从URL中提取文件名
else {
fileName = url.getFile().substring(url.getFile().lastIndexOf("/") + 1, url.getFile().length());
}
// 获取文件输入流和输出流
InputStream inputStream = httpConn.getInputStream();
OutputStream outputStream = new FileOutputStream("C:\\downloads\\" + fileName);
// 将输入流写入输出流,实现文件下载
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 关闭输入流和输出流
outputStream.close();
inputStream.close();
System.out.println("文件下载完成");
} else {
System.out.println("文件下载失败,HTTP响应码:" + responseCode);
}
httpConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个示例代码使用HttpURLConnection发送GET请求获取文件输入流,然后将输入流写入文件输出流。通过设置Content-Disposition头部可以获取到文件名,如果无法获取则从URL中提取文件名。文件下载成功后关闭输入流和输出流。
java 实现浏览器下载文件
要在 Java 中实现浏览器下载文件,可以使用 Java 的 URL 和 HttpURLConnection 类。以下是实现的步骤:
1. 构建 URL 对象,指定要下载的文件的 URL。
2. 打开 HttpURLConnection 连接,并设置请求头,指定请求方式为 GET。
3. 获取 HttpURLConnection 的输入流,读取文件内容。
4. 将文件内容写入到本地文件中。
示例代码:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileDownload {
public static void main(String[] args) {
String fileUrl = "http://example.com/file.pdf"; // 要下载的文件 URL
String savePath = "/path/to/save/file.pdf"; // 本地保存文件的路径
try {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStream inputStream = conn.getInputStream();
BufferedInputStream in = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(savePath);
BufferedOutputStream out = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
conn.disconnect();
System.out.println("文件下载完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码将会从指定的 URL 下载文件,并保存到指定的本地路径。