0

I'm using Java to download a file, I have used three different code snippets that I'm going to post here. But the result for all is same. They are all downloading the file partially and as a result the file can't be opened as it is not downloaded completely. How I should fix this problem.

My first code:

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();
            }
        }
 }






}

Second code I used:

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());
            }

}

}

the third code I used:

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();
    }
}



}

I have to download a video file, which is about 6-7 MB, however I have used these three codes for small text files and other types, it didn't work. Any one have any idea?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    *"It didn't work"* - Please explain *how* it / they didn't work. Compilation errors? Runtime errors? Something else? What were the error messages and what other evidence can you provide? – Stephen C Jul 18 '15 at 03:13
  • 1
    For what it is worth, the 3 snippets have *different* hardwired URLs, and output files. If you copied them off the internet and didn't modify them, they are unlikely to work for you. – Stephen C Jul 18 '15 at 03:16
  • There is no errors , the program is run. no compilation and no run errors. The file is downloaded partially. I can see the file after running the pro gram in the directory I give. if it's 2 MB file , it will download only 80-90 KB of it. I have used real URLS with the files in that url. any idea? – mehrnaz bayaki Jul 18 '15 at 03:26
  • I suspect that there is something wrong with either the server or the file you are trying to download. Or maybe, the server is doing that you deliberately as an "anti-scraping" or "anti-pirating" strategy. – Stephen C Jul 18 '15 at 03:51
  • It would help if you showed us the real code and the real URLs that you are using. My idea is that the problem is in something you are not showing us. The code (at least for the first version) looks correct to me. – Stephen C Jul 18 '15 at 03:54
  • It could be, but I have used different urls and different files. I don't know what is going wrong. Thanks for ur answer. – mehrnaz bayaki Jul 18 '15 at 03:57
  • One of the url I used as I show here is : https://archive.org/details/alanoakleysmalltestvideo the name of the file is: spacetestSMALL.wmv and the directory I save in my pc : "C:\\Users\\Mehrnaz\\Downloads" – mehrnaz bayaki Jul 18 '15 at 04:01
  • Your first and third samples will work. The second will only work on text files that fit into memory, and wastes both time and space. – user207421 Jul 18 '15 at 08:44

2 Answers2

3

I tried your first example and it works for me. I guess you are misinterpreting what you are actually doing. With

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

you are downloading the HTML page of textfiles.com/holiday to a file called "holiday.tar.gz". The actual tar archive has the URL archives.textfiles.com/holiday.tar.gz.

In one of your comments you say you actually want to download a video under archive.org/details/alanoakleysmalltestvideo, but using this URL you simply download the HTML page (successfully) where the video is embedded.

If you look into the HTML sources, you can find an actual video URL, e.g., archive.org/download/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4, and successfully download it with your existing code.

fhissen
  • 347
  • 2
  • 7
  • I think you are right. you give me the answer. However now the code is not downloading the file partially, now it is not downloded at all. and it will give me (Access is denied) FileNotFoundException. this is related to access to folders on my system , which I can't solve it. Do you know how to solve that? thanks. – mehrnaz bayaki Jul 18 '15 at 08:45
  • Do you know how to solve this. I need to solve this by the end of tomorrow. Please help me if any one knows. – mehrnaz bayaki Jul 18 '15 at 09:08
  • My problem is solved. I had a bad interpretation from URL as you told me fhissen. I fixed that now my url is pointing to the file: "http://ia601409.us.archive.org/8/items/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4" – mehrnaz bayaki Jul 18 '15 at 09:38
-1

I had a bad interpretation from URL as you told me fhissen. I fixed that now my url is pointing to the file: "http://ia601409.us.archive.org/8/items/alanoakleysmalltestvideo/spacetestSMALL_512kb.mp4" using the first code snippet, I have: outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "spacetestSMALL_512kb.mp4")); and the destinationDir is : "C:\download\" . After I solved this problem another error I had as FileNotFoundException access denied. This is about the permission on the folder I want to download the file in to it. So I gave my self full permission on the folder and I also did that for the Java folder in Program Files as mentioned in this page how to do it : Access is denied java.io.FileNotFoundException . This is now working using first code, I think code 2 and 3 will work too, but anyone can test it. Now I can download the file properly. Thanks everyone :-)

Community
  • 1
  • 1