使用Java代码

时间:2015-07-18 02:57:04

标签: java

我正在使用Java下载文件,我使用了三个不同的代码片段,我将在此处发布。但是所有人的结果都是一样的。他们都在部分下载文件,因此无法打开文件,因为它没有完全下载。我应该如何解决这个问题。

我的第一个代码:

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的视频文件,但是我已经将这三个代码用于小文本文件和其他类型,但它不起作用。有人有任何想法吗?

2 个答案:

答案 0 :(得分:3)

我尝试了你的第一个例子,它对我有用。我猜你是在误解你实际在做什么。与

fileUrl("http://textfiles.com/holiday","holiday.tar.gz","C:\\Users\\Me\\Downloads");

您正在将textfiles.com/holiday的HTML页面下载到名为“holiday.tar.gz”的文件中。实际的tar存档包含URL archives.textfiles.com/holiday.tar.gz。

在您的一条评论中,您说您确实想要在archive.org/details/alanoakleysmalltestvideo下载视频,但使用此网址只需下载嵌入视频的HTML页面(已成功)。

如果查看HTML源代码,您可以找到实际的视频网址,例如archive.org/download/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4,并使用现有代码成功下载。

答案 1 :(得分:-1)

当你告诉我fhissen时,我对URL的解释很糟糕。我修复了现在我的网址指向文件:“http://ia601409.us.archive.org/8/items/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4”使用第一个代码片段,我有:outStream = new BufferedOutputStream(new             FileOutputStream(destinationDir +“spacetestSMALL_512kb.mp4”)); destinationDir是:“C:\ download \”。在我解决了这个问题后,我遇到了另一个错误,因为FileNotFoundException访问被拒绝了。这是关于我想要将文件下载到其中的文件夹的权限。所以我给了我自己对该文件夹的完全权限,我也为本文提到的Program Files中的Java文件夹做了如何操作:Access is denied java.io.FileNotFoundException。现在使用第一个代码,我认为代码2和3也可以工作,但任何人都可以测试它。现在我可以正确下载文件了。谢谢大家: - )