0
私のコードを実行すると、設定したバイト範囲の結果が得られません。私は、ダウンロードするには、このリンクを使用します。https://docs.oracle.com/javaee/7/JEETT.pdfURLコンテンツの範囲または範囲を取得する方法
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();/
}
}