【发布时间】:2015-07-18 02:57:04
【问题描述】:
我正在使用 Java 下载文件,我使用了三个不同的代码 sn-ps,我将在此处发布。但所有人的结果都是一样的。他们都在部分下载文件,因此无法打开文件,因为它没有完全下载。我应该如何解决这个问题。
我的第一个代码:
public class FileDownloader {
final static int size=1024;
public static void main(String[] args){
fileUrl("http://textfiles.com/holiday","holiday.tar.gz","C:\\Users\\Me\\Downloads");
}
public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL Url;
byte[] buf;
int ByteRead,ByteWritten=0;
Url= new URL(fAddress);
outStream = new BufferedOutputStream(new
FileOutputStream(destinationDir+"\\"+localFileName));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
}catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
我使用的第二个代码:
public class downloadFile {
public static void main(String[]args){
downloadFile dfs = new downloadFile();
dfs.downloadFileAndStore("C:\\Users\\Me\\Downloads","Sign&ValidateVersion2.docx");
}
/**
* download and save the file
* @param fileUrl: String containing URL
* @param destinationDirectory : directory to store the downloaded file
* @param fileName : fileName without extension
*/
public void downloadFileAndStore(String destinationDirectory,String fileName){
// URL url = null;
FileOutputStream fos = null;
//convert the string to URL
try {
// url = new URL(fileUrl);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://mail.uvic.ca/owa/#path=/mail");
HttpResponse response = client.execute(request);
if(response.getEntity().getContent().read()==-1){
Log.error("Response is empty");
}
else{
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
fos = new FileOutputStream(destinationDirectory + "\\" + fileName);
fos.write(result.toString().getBytes());
fos.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
}
}
}
我使用的第三个代码:
public class download {
public static void main(String[] args) {
download dfs = new download();
dfs.downloadFile();
}
public void downloadFile(){
try {
IOUtils.copy(
new URL("https://archive.org/details/alanoakleysmalltestvideo").openStream(),
new FileOutputStream("C:\\Users\\Me\\Downloads\\spacetestSMALL.wmv")
);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我必须下载一个大约 6-7 MB 的视频文件,但是我将这三个代码用于小文本文件和其他类型的文件,它不起作用。有人知道吗?
【问题讨论】:
-
“它不起作用” - 请解释如何它/它们不起作用。编译错误?运行时错误?还有什么?您可以提供哪些错误消息以及您可以提供哪些其他证据?
-
值得一提的是,这 3 个 sn-ps 具有 不同 硬连线 URL 和输出文件。如果您从互联网上复制它们并且没有修改它们,它们不太可能适合您。
-
没有错误,程序运行。没有编译,也没有运行错误。该文件已部分下载。在我提供的目录中运行程序后,我可以看到该文件。如果是 2 MB 文件,它只会下载 80-90 KB。我已经将真实的 URL 与该 URL 中的文件一起使用。有什么想法吗?
-
我怀疑服务器或您尝试下载的文件有问题。或者,服务器故意这样做是为了作为“反抓取”或“反盗版”的策略。
-
如果您向我们展示您正在使用的真实代码和真实 URL,将会有所帮助。我的想法是问题出在你没有向我们展示的东西上。代码(至少对于第一个版本)对我来说是正确的。
标签: java