簡體   English   中英

使用Java代碼未完全下載文件

[英]File is not downloaded completely using Java code

我正在使用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的視頻文件,但是我已經將這三個代碼用於小型文本文件和其他類型,但是沒有用。 有人知道嗎?

我嘗試了您的第一個示例,它對我有用。 我猜您在誤解您的實際操作。

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下下載視頻,但是使用此URL,您只需(成功)下載嵌入視頻的HTML頁面。

如果您查看HTML源代碼,則可以找到實際的視頻URL,例如archive.org/download/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4,並使用現有代碼成功下載。

當您告訴我菲森時,我對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文件夾進行了相應操作:如何拒絕訪問:java.io.FileNotFoundException 現在可以使用第一個代碼來工作,我認為代碼2和3也可以工作,但是任何人都可以對其進行測試。 現在,我可以正確下載文件了。 感謝大家 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM