如何获取网址内容的范围或范围

时间:2016-09-14 10:30:56

标签: java url http-headers

当我运行我的代码时,它不会在我设置的字节范围内给出结果。我使用此链接下载:https://docs.oracle.com/javaee/7/JEETT.pdf

public class DownloadFileFromURL { 
    private static final int BUFFER_SIZE = 1024;
    private static String s;
    private static URL url;
    private static int mstart,mend,size;
    HttpURLConnection conn = null;
    BufferedInputStream bis = null;
    RandomAccessFile raf = null ;
    String bytesrange ;
    byte[] buffer = new byte[BUFFER_SIZE];

    File f ;
    //get content lenght
    //chia ra 3 thread?

    public void getContentLength() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Input URL :");
        s = sc.nextLine();
        try {
            url = new URL(s);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*conn.connect();*/
        int contentlen = conn.getContentLength();
        size = contentlen;
        mstart = 0;
        mend = size/3;
         bytesrange = mstart + "-" + mend ;
        //conn.disconnect();
    }
        // public void getFileName(File f){     

        //}


    public class Thread1 implements Runnable {
        public void run() {
            try {
                String fn = url.getFile();
                String fn2 = fn.substring(fn.lastIndexOf('/') +1);
                File f = new File("/home/k10/Downloads/" +fn2 + ".part1");
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                //  conn.setRequestProperty("Accept-Ranges","bytes");
                //conn.setRequestProperty("Range","bytes="+bytesrange);
                conn.setRequestProperty("Range","bytes=" +bytesrange);

                bis = new BufferedInputStream(url.openStream());
                raf = new RandomAccessFile(f, "rw");
                raf.seek(mstart);           
                int count = 0 ;
                while ((count = bis.read(buffer,0,BUFFER_SIZE)) != -1){
                    raf.write(buffer,0,count);
                    mstart += count;                                
                }
                bis.close();
                raf.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
    }

    public static void main(String[] args)  {       
                new DownloadFileFromURL().getContentLength();
        DownloadFileFromURL.Thread1 th1 = new DownloadFileFromURL().new Thread1();
        DownloadFileFromURL.Thread2 th2 = new DownloadFileFromURL().new Thread2();
        DownloadFileFromURL.Thread3 th3 = new DownloadFileFromURL().new Thread3();
        th1.run();
        //th2.run();
        //th3.run();/
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码的问题是您从urlopenStream()创建BufferedInputStream:

        bis = new BufferedInputStream(url.openStream());

而是需要连接HttpURLConnection,然后从连接中获取输入流。

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Range", "bytes=" + bytesrange);
        conn.connect();
        bis = new BufferedInputStream(conn.getInputStream());

然后你会得到部分回应。