2015년 1월 27일 화요일

[Java] File download.(BufferInputStream)

Java File Download source.



inputstream과 outputstream을 사용.
try {
         URL url=new URL("URL");
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         int filesize = connection.getContentLength();
         float totalDataRead=0;
         java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
         java.io.FileOutputStream fos = new java.io.FileOutputStream("C:/temp.zip");
         java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) {
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i);
               float Percent=(totalDataRead*100)/filesize;
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }

explane
try {
         URL url=new URL("URL");  //File download adress
         HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //connect URL
         int filesize = connection.getContentLength(); //파일 사이즈를 가져옵니다.
         float totalDataRead=0; // totalData read (byte)
         BufferedInputStream in = newBufferedInputStream(connection.getInputStream()); //read File from url
         OutputStream fos = new FileOutputStream("C:/temp.zip"); //save adress.
         BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) { //Start Downloading
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i); //download
               float Percent=(totalDataRead*100)/filesize; //Percent of Download.
               System.out.println(Percent);
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }
ㅃㅃ.
 

댓글 없음:

댓글 쓰기