public void normal(String link) throws MalformedURLException, IOException{
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
 
        // connect to the server
        URL url = new URL(link);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.connect();
 
        // prepare for read from server
        in = new BufferedInputStream(conn.getInputStream());
         
        // prepare for write to local disk       
        String fileName = "TempFile";
        out = new BufferedOutputStream(new FileOutputStream(fileName));
        int n;
         
        // read from the server and write to local storage
        byte data[] = new byte[1024];
        while( (n = in.read(data)) != -1){
            out.write(data, 0, n);
        }
        in.close();
        out.close();
    }
package workingonnetworking;
 
import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class DownloadManager {
 
    private OutputStream[] r;
    private final int totalChunks = 5;
    private File[] tempFile;
    public DownloadManager() throws FileNotFoundException{
        r = new OutputStream[totalChunks];
        tempFile = new File[totalChunks];
         
        for (int i = 0 ; i < totalChunks ; i++){
            tempFile[i] = new File("temp"+i);
            r[i] = new BufferedOutputStream(new FileOutputStream(tempFile[i]));
        }
         
    }
    public static void main(String[] args) throws IOException {
        DownloadManager cc = new DownloadManager();
//        cc.normal(http://localhost:31415/public_html/img/textile.jpg);
        cc.parallel("http://localhost:31415/public_html/img/textile.jpg");
    }
    public void normal(String link) throws MalformedURLException, IOException{
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
 
        // connect to the server
        URL url = new URL(link);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.connect();
 
        // prepare for read from server
        in = new BufferedInputStream(conn.getInputStream());
         
        // prepare for write to local disk       
        String fileName = getFileName(link);
        out = new BufferedOutputStream(new FileOutputStream(fileName));
        int n;
         
        // read from the server and write to local storage
        byte data[] = new byte[1024];
        while( (n = in.read(data)) != -1){
            out.write(data, 0, n);
        }
        in.close();
        out.close();
    }
     
     
    public void parallel(String link){
        try {
            // open up a connection to get total size of the file
            URL url = new URL(link);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            int total = conn.getContentLength();
             
            // connection array
            HttpURLConnection[] con = new HttpURLConnection[totalChunks];
            Minions[] m = new Minions[totalChunks];
             
            // calculate bytes each stream has to download
            int eachDownload = total/totalChunks;                        
            String byteRange = "";
            ExecutorService tE = Executors.newCachedThreadPool();
             
            for (int i = 0 ; i < totalChunks ; i++){
                // open the connection streams with byte range
                byteRange = (eachDownload*i)+"-"+((i!=totalChunks-1)?(eachDownload*(i+1)-1):total);
                con[i] = (HttpURLConnection) url.openConnection();
                con[i].setRequestProperty("Range", "bytes="+byteRange);
                con[i].connect();
                 
                // if server doesn't support partial content
                if (con[i].getResponseCode() != 206){
                    System.out.println("Parallel Stream is not supported by the server");
                    this.normal(link);
                    break;
                }
                 
                // Fire the threads to do the job.
                m[i] = new Minions(con[i], r[i]);
                tE.execute(m[i]);
                 
            }
            tE.shutdown();                                  // no new threads are to be created
            tE.awaitTermination(10000, TimeUnit.DAYS);      // wait untill all the threads terminates
                         
            File dest = new File(getFileName(link));
            String filePath = mergeFiles(dest, tempFile);
            System.out.println("Download File Completed.");
            System.out.println("File has been saved to " + filePath);
             
        } catch (InterruptedException ex) {
            Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private String mergeFiles(File dest, File[] files){
        try {
            byte[] reader = new byte[1024*5];
            int n;
            InputStream is;
            OutputStream os;
             
            os = new BufferedOutputStream(new FileOutputStream(dest));
            for (File iter : files){
                is = new BufferedInputStream(new FileInputStream(iter));
                while((n = is.read(reader)) != -1){
                    os.write(reader, 0, n);
                }
            }
 
            os.flush();
            os.close();
        } catch (IOException ex) {
            Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dest.getAbsolutePath();
    }
     
    // get filename out of the link unable to find one returns 'DownloadedFile' as default
    private String getFileName(String link){
        String fileName = link.substring(link.lastIndexOf("/"), link.length());
        if (fileName.isEmpty()) fileName = "DownloadedFile";
        return fileName;
    }
     
    // these are the threads fetching each of chunks
    private class Minions implements Runnable{
        HttpURLConnection con;
        OutputStream r;
        public Minions(HttpURLConnection acon, OutputStream ar){
            con = acon;
            r = ar;
        }
 
        @Override
        public void run(){
            try {
                BufferedInputStream in = new BufferedInputStream(con.getInputStream());
                int numRead;
         
                byte data[] = new byte[1024];
                while( (numRead = in.read(data, 0, 1024)) != -1){
                    r.write(data, 0, numRead);
                    r.flush();
                }
                 
                r.close();
            } catch (IOException ex) {
                Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);
            }
             
        }
    }
}
 
kada boro kada!!!
ReplyDelete