내가 생각하는 것처럼 FileOutputStream
이 작동한다는 일부 확인을 찾고 있습니다. 파일을 다운로드하는 중이고 네트워크 연결이 끊어지면 다운로드를 다시 시작하려고합니다.FileOutputStream 헤드 포인터
이 작업을 수행하는 방법은 FileOutputStream을 추가하지 않고 오프셋으로 아무 것도 쓰지 않는 것으로 열어 보는 것입니다. 내 질문은 그 작품이 될 것인가 아니면 추가하지 않는 것으로 열어 내용을 삭제합니까? 또한 내가 오프셋에서 쓸 경우 후속 호출에서 파일의 해당 위치 이후에 계속 쓰기가 가능합니까?
File outFile = new File(outFileName);
FileOutputStream out = new FileOutputStream(outFile);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
long fileSize = connection.getContentLength();
int status = DOWNLOADING;
connection.connect();
InputStream in = connection.getInputStream();
long downloaded = in.skip(outFile.length());
publishProgress(downloaded, fileSize);
try
{
int read = 0;
byte buffer[] = new byte[MAX_BUFFER_SIZE];
// Skip ahead in the out buffer
out.write(buffer, (int)downloaded, read);
while(status == DOWNLOADING)
{
if(!NetworkUtils.isConnected(_context))
{
// This breaks us out of the doInBackground in the AsyncTask
_downloadFailed = true;
throw new Exception("Network Connectivity Lost!");
}
read = in.read(buffer);
if(read == -1)
{
publishProgress(fileSize);
break;
}
out.write(buffer, 0, read);
downloaded += read;
publishProgress(downloaded);
}
}
finally
{
out.close();
in.close();
connection.disconnect();
}
도움이 될 것입니다! 감사! – CaseyB