File not getting downloaded from internet.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    File not getting downloaded from internet.

    Here is my code

    Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.SocketAddress;
    import java.net.URL;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.net.URLConnection;
    
    /**
     *
     * @author Admin
     */
    public class SWfDowloader {
        public static void main(String a[]) throws Exception{
            String swf_path = "http://www.prototypejs.org/assets/2008/1/25/prototype-1.6.0.2.js";
            URL swf_url = new URL(swf_path);
            byte address[] = {(byte)144,(byte)16,(byte)192,(byte)245};
            SocketAddress socket_address = new InetSocketAddress(InetAddress.getByAddress(address),8080);
            Proxy proxy = new Proxy(Proxy.Type.HTTP,socket_address);
            URLConnection url_conn = swf_url.openConnection(proxy);
            url_conn.connect();
            BufferedInputStream swf_in_stream = new BufferedInputStream(url_conn.getInputStream());
            FileOutputStream swf_file = new FileOutputStream("d:/test.js");
            BufferedOutputStream swf_out_stream = new BufferedOutputStream(swf_file);
            byte bytes[] = new byte[512];
            int read_bytes = 0;
            while((read_bytes=swf_in_stream.read(bytes))!=-1){
                System.out.println(read_bytes);
                swf_out_stream.write(bytes, 0,read_bytes);
            }
            swf_in_stream.close();
            swf_file.close();
            swf_out_stream.close();
        }
    }
    The file is not getting downloaded properly some parts left at the end what should i do?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You're closing the FileOutputStrea m before you're closing the wrapping BufferedOutputS tream so something could have been in that buffer that could not be flushed to the wrapped file stream. Close the BufferedOutputS tream first (it will close the wrapped stream for you).

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Wow what an experience ;)
      The file now getting downloaded properly.
      But if i want to download video stream then will my code help that?
      "http://s.ytimg.com/yt/swf/watch-vfl87635.swf" this is my link.
      Actually file is downloaded but it's now playing properly, no video is coming ;)

      Actually what happens if i put the URL of that JS file then the content comes on the browser, but if i put the video URL then no video stream is coming.
      What would be the procedure to download the video stream in this case?

      Comment

      Working...