基于java下载中getcontentlength()一直为-1
时间: 2023-09-04 18:01:06 浏览: 73
getcontentlength()是java中URLConnection类的一个方法,用于获取从指定URL下载文件的内容长度。当getcontentlength()返回-1时,表示无法获取文件的内容长度。
出现这种情况可能有几种原因。首先,可能是下载文件的URL链接不正确或无效,导致服务器无法获取文件的正常长度信息。其次,有些服务器可能没有配置好,无法提供正确的Content-Length头字段,因此无法获取到文件的内容长度。另外,如果下载文件的服务器使用了分块传输编码(chunked transfer encoding)方式,那么getcontentlength()方法也不能获取到正确的内容长度。
解决这个问题的一个方法是,可以通过获取下载文件的输入流,使用流读取的方式来获取文件的内容长度。可以使用InputStream.available()方法来获取输入流中的可读取字节数,然后根据字节数计算出文件的内容长度。
另外,还可以尝试使用其他的下载库或工具来替代URLConnection类来进行文件下载。例如,可以使用Apache HttpClient或OkHttp等第三方库来发送HTTP请求,并获取到文件的正确的内容长度。
总之,当getcontentlength()方法返回-1时,意味着无法获取到文件的内容长度。需要进行其他的方式来获取文件的长度,或尝试使用其他的下载工具来解决这个问题。
相关问题
Java从服务器读取文件并下载到内存中
可以使用Java的IO流来读取服务器上的文件,并将其下载到内存中。以下是一个简单的示例代码:
```java
import java.io.*;
import java.net.*;
public class FileDownloader {
public static void main(String[] args) {
String fileUrl = "http://example.com/file.txt";
String saveDir = "/path/to/save/directory/";
try {
URL url = new URL(fileUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// Check if the response is OK
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// Extract the file name from the content disposition header
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
} else {
// Extract the file name from the URL
fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length());
}
// Open input stream
用Java实现超过1g的zip文件下载功能
您可以使用Java中的URLConnection和InputStream来实现超过1G的zip文件下载功能。以下是一个简单的示例代码:
```java
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class ZipFileDownloader {
public static void main(String[] args) {
String fileURL = "https://example.com/largefile.zip"; // 下载文件的URL
String saveDir = "/path/to/save/directory"; // 保存文件的目录
try {
downloadFile(fileURL, saveDir);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void downloadFile(String fileURL, String saveDir) throws IOException {
URL url = new URL(fileURL);
URLConnection conn = url.openConnection();
conn.connect();
// 获取文件大小
int fileSize = conn.getContentLength();
// 创建输入流并设置缓冲区大小
InputStream in = new BufferedInputStream(conn.getInputStream(), 4096);
// 创建输出流
FileOutputStream out = new FileOutputStream(saveDir + "/" + "largefile.zip");
byte[] buffer = new byte[4096];
int bytesRead = -1;
long totalBytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
System.out.println("已下载: " + (totalBytesRead * 100 / fileSize) + "%");
}
out.close();
in.close();
System.out.println("下载完成!");
}
}
```
此代码将从指定的URL下载zip文件,并将其保存到指定的目录中。它使用缓冲输入流和缓冲输出流来提高下载速度,并在下载过程中输出已完成的百分比。