Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java中的url下载文件不完整-使用java NIO_Java - Fatal编程技术网

从java中的url下载文件不完整-使用java NIO

从java中的url下载文件不完整-使用java NIO,java,Java,我使用以下线程的可接受解决方案中提供的代码下载了一个500kb的zip文件 代码能够识别文件,但问题是,下载不完整。它总是下载5KB,然后停在那里 我不想一直使用apache commons文件。问题是我没有设置身份验证。我设置了身份验证,它成功了。代码如下 public static File downloadFile(String fileURL, String saveDir) throws IOException { File downloadFolder = null;

我使用以下线程的可接受解决方案中提供的代码下载了一个500kb的zip文件

代码能够识别文件,但问题是,下载不完整。它总是下载5KB,然后停在那里


我不想一直使用apache commons文件。

问题是我没有设置身份验证。我设置了身份验证,它成功了。代码如下

public static File  downloadFile(String fileURL, String saveDir)
    throws IOException {

File downloadFolder = null;
String saveFilePath = null;

String username = "guest";
String password = "guest";

String usepass = username + ":" + password;
String basicAuth = "Basic "+  javax.xml.bind.DatatypeConverter.printBase64Binary(usepass.getBytes());

URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) website.openConnection();
httpConn.setRequestProperty("Authorization", basicAuth);

ReadableByteChannel rbc = Channels.newChannel(httpConn.getInputStream());
String    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,fileURL.length()); 
FileOutputStream fos = new FileOutputStream(fileName);

saveFilePath = saveDir + File.separator + fileName;
downloadFolder = new File(saveDir);

downloadFolder.deleteOnExit();
downloadFolder.mkdirs();

FileOutputStream outputStream = new FileOutputStream(saveFilePath);
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

outputStream.close();
rbc.close();
return new File(saveFilePath);

}

您能获得下载文件的大小吗?您的意思是在下载之前读取文件大小吗?很抱歉,您无法读取ReadableByteChannel的大小。使用read方法直到返回值为-1,然后将缓冲区写入文件。实际上,我使用了传统的读取缓冲区的方法,直到-1,同样的原因也失败了。只有5kb的下载。我尝试使用ApacheCommons io只是为了进行实验,即使是5k下载也会停止。我认为是其他原因导致了这个问题。当使用浏览器下载时,超过5KB?
public static File  downloadFile(String fileURL, String saveDir)
    throws IOException {

File downloadFolder = null;
String saveFilePath = null;

String username = "guest";
String password = "guest";

String usepass = username + ":" + password;
String basicAuth = "Basic "+  javax.xml.bind.DatatypeConverter.printBase64Binary(usepass.getBytes());

URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) website.openConnection();
httpConn.setRequestProperty("Authorization", basicAuth);

ReadableByteChannel rbc = Channels.newChannel(httpConn.getInputStream());
String    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,fileURL.length()); 
FileOutputStream fos = new FileOutputStream(fileName);

saveFilePath = saveDir + File.separator + fileName;
downloadFolder = new File(saveDir);

downloadFolder.deleteOnExit();
downloadFolder.mkdirs();

FileOutputStream outputStream = new FileOutputStream(saveFilePath);
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

outputStream.close();
rbc.close();
return new File(saveFilePath);