2016-09-14 4 views
0

Wenn ich meinen Code ausführe, wird das Ergebnis nicht im von mir festgelegten Bytebereich angezeigt. Ich benutze diesen Link heruntergeladen werden: https://docs.oracle.com/javaee/7/JEETT.pdfSo erhalten Sie den Bereich oder Bereich des URL-Inhalts

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

Antwort

0

Das Problem mit Ihrem Code ist, dass Sie die BufferedInputStream von urlopenStream() zu erstellen:

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

stattdessen müssen Sie die HttpURLConnection verbinden und dann bekommen die Eingangs streamen von der Verbindung.

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

Dann erhalten Sie eine teilweise Antwort.

Verwandte Themen