안녕하세요 httpurlconnection으로 파일링크 ex)http://주소//파일명.확장자;
를 통해 다운로드 받는 기능을 구현 하고있습니다.
현재 이어받기를 구현하려고 하는데요, 찾아보니 RandomAccessFile로
특정위치에 값을 새로 쓸 수 있다고 하더라구요.
대략 6.8MB가 되는 파일로 테스트를 해봤는데,
매번 0부터 덮어씌워져 write되는 것 같아 질문글 올립니다.
그래서 저장-취소를 반복할 때마다 구간을 달리 취소하면 2MB였다가, 5MB,1MB
다양하게 파일크기가 바뀌네요.
제가 이해한 바로는 0에서 3mb정도 write하다가 취소하고 다시 작업을 실행할 때
앞서 끊겼던 3mb위치부터 작업을 시작하는걸로 알고있거든요.
혹시 소스에서 제가 잘못구현한 부분이 있는지 같이 올립니다.
File kFile = new File(path + "/" + nowfile);
RandomAccessFile ra = new RandomAccessFile(kFile.getAbsolutePath(), "rw");
myfileSize=ra.length();
ra.seek(myfileSize); //현재 size부터
URL myUrl = new URL(urlStr);
conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestProperty("Accept-Ranges", "bytes");
conn.setRequestProperty("Range", "bytes="+String.valueOf(myfileSize)+'-');
conn.connect();
conn.setConnectTimeout(2000);// 서버 접속 시 연결 시간.
conn.setReadTimeout(2000);// READ 시 연결 시간.
myremains = conn.getContentLength(); //파일 총 사이즈
mylengthOfFile = myremains+myfileSize;
fosFile = new FileOutputStream(kFile);
byte[] data = new byte[1024];// lenth
int read;
long total = 0;
int progress = 0;
InputStream is = conn.getInputStream();
if(myfileSize<mylengthOfFile)
{
for (;;) {
read = is.read(data);
total += read;
int progress_temp = (int) ((double) total / myfileSize * 100);
publishProgress("" + progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp)
{
progress = progress_temp;
}
if (read <= 0)
{
break;
}
fosFile.write(data, 0, read);