无法从java中的URL下载文件 [英] Cannot download file from URL in java
本文介绍了无法从java中的URL下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class FileDownloader {
public static void main String [] args)throws IOException {
InputStream fileIn;
FileOutputStream fileOut;
扫描仪s =新扫描仪(System.in);
System.out.println(Enter URL:);
String urlStr = s.nextLine();
URL url = new URL(urlStr);
URLConnection urlConnect = url.openConnection();
fileIn = urlConnect.getInputStream();
System.out.println(输入文件名:);
String fileStr = s.nextLine();
fileOut = new FileOutputStream(fileStr);
while(fileIn.read()!= -1){
fileOut.write(fileIn.read());
}
System.out.println(File is downloaded);
}
}
那么我该如何解决呢?应该使用另一种方式下载?
解决方案
您正在丢失每个备用的字节
while(fileIn.read()!= -1){//第一次读
fileOut。写(fileIn.read()); //第二读 - 第一写
}
你正在阅读两次,只写一次。
您需要做的是
int x;
while((x = fileIn.read())!= -1){//第一次读取
fileOut.write(x); //第一个写
}
这是您的完整代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class FileDownloader {
public static void main(String [] args)throws IOException {
InputStream fileIn;
FileOutputStream fileOut;
扫描仪s =新扫描仪(System.in);
System.out.println(Enter URL:);
String urlStr = s.nextLine();
URL url = new URL(urlStr);
URLConnection urlConnect = url.openConnection();
fileIn = urlConnect.getInputStream();
System.out.println(输入文件名:);
String fileStr = s.nextLine();
fileOut = new FileOutputStream(fileStr);
int x;
while((x = fileIn.read())!= -1){
fileOut.write(x);
}
System.out.println(File is downloaded);
}
I'm making a program that will download files from URL. The downloading always starts, but it is not completed. For example, if file's size is 3 MB, program download only half of that so I cannot open the downloaded file. But program says that file is downloaded succesfully.
public class FileDownloader {
public static void main (String [] args) throws IOException {
InputStream fileIn;
FileOutputStream fileOut;
Scanner s = new Scanner(System.in);
System.out.println("Enter URL: ");
String urlStr = s.nextLine();
URL url = new URL(urlStr);
URLConnection urlConnect = url.openConnection();
fileIn = urlConnect.getInputStream();
System.out.println("Enter file name: ");
String fileStr = s.nextLine();
fileOut = new FileOutputStream(fileStr);
while (fileIn.read() != -1) {
fileOut.write(fileIn.read());
}
System.out.println("File is downloaded");
}
}
So how can I solve it? Should use another way to download?
解决方案
You are losing every alternate byte
due to
while (fileIn.read() != -1) { //1st read
fileOut.write(fileIn.read()); //2nd read - 1st write
}
You are reading twice and writing only once.
What you need to do is
int x;
while ((x = fileIn.read()) != -1) { //1st read
fileOut.write(x); //1st write
}
Here is your complete code
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class FileDownloader {
public static void main(String[] args) throws IOException {
InputStream fileIn;
FileOutputStream fileOut;
Scanner s = new Scanner(System.in);
System.out.println("Enter URL: ");
String urlStr = s.nextLine();
URL url = new URL(urlStr);
URLConnection urlConnect = url.openConnection();
fileIn = urlConnect.getInputStream();
System.out.println("Enter file name: ");
String fileStr = s.nextLine();
fileOut = new FileOutputStream(fileStr);
int x;
while ((x = fileIn.read()) != -1) {
fileOut.write(x);
}
System.out.println("File is downloaded");
}
这篇关于无法从java中的URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文