具体下载的代码:
long endPos = bean.getEndLen();
long startPos = bean.getStartLen();
System.out.println(current.getId()
+ "-------------------当前线程ID--------当前线程临时文件" + itemFile.getClass().getName()
+ "-----文件的开始和结束位置----" + startPos + "---" + endPos);
while (endPos > startPos && !isDownloadOver) {
try {
URL url = new URL(bean.getFileUrl());
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
InputStream is = new BufferedInputStream(conn
.getInputStream());
byte[] buff = new byte[BUFF_LENGTH];
int length = -1;
while((length = is.read(buff)) > 0
&& startPos < endPos && !isDownloadOver) {
startPos += itemFile.write(buff, 0, length);
// 写入文件下载量
writePoint(bean, startPos + "");
}
is.close();
conn.disconnect();
System.out.println("分线程号:"+current.getId()+"下载完成");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//写文件的类
public class FileAccess implements Serializable {
private RandomAccessFile oSavedFile;
@SuppressWarnings("unused")
private long nPos;
public FileAccess() throws IOException {
this("");
}
public FileAccess(String sName) throws IOException {
this.oSavedFile = new RandomAccessFile(sName, "rw");
this.nPos = oSavedFile.length();
this.oSavedFile.seek(nPos);
}
/**
* 分写内容
* @param b
* @param nStart
* @param nLen
* @return
*/
public int write(byte[] b, int nStart, int nLen) {
int n = -1;
try {
this.oSavedFile.write(b,nStart,nLen);
n = nLen;
} catch(IOException e) {
e.printStackTrace();
}
return n;
}
public long getSize() throws IOException{
return oSavedFile.length();
}
public void close() throws IOException{
oSavedFile.close();
oSavedFile = null;
}
}
问题:如果不断,直接一次性下载文件就可以打开,但是如果断点后,在连的化,文件就无法打开了