添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
privatebooleandownloadFile(StringsURL,StringsName){FilefTile=newFile(sPath+"\\"+sName... private boolean downloadFile(String sURL, String sName) {
File fTile = new File(sPath + "\\" + sName);
if (fTile.exists()) {
return true;
}

int nStartPos = 0;
int nRead = 0;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
// int state = httpConnection.getResponseCode();
// if(state != 200)
// return false;

long nEndPos = getFileSize(sURL);
if (nEndPos < 0 || nEndPos == 2407)
return false;

RandomAccessFile oSavedFile = new RandomAccessFile(sPath + "\\" + sName, "rw");
httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
httpConnection.setRequestProperty("RANGE", sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
oSavedFile.close();
httpConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
def downloadFile(self,sURL,sName):
        import os.path
        if os.path.exists(os.path.join(self.sPath,sName )):
            return True
        nEndPos=self.getFileSize(sURL)
        if nEndPos<0 or nEndPos == 2407:
            return False
try:
import urllib2
req = urllib2.Request(sURL)
req.add_header("User-Agent", "Internet Explorer")
req.add_header("RANGE","bytes=" + str(0)+ "-")
res = urllib2.urlopen(req)
with open(os.path.join(self.sPath,sName ),'wb') as f:
while True:
data = res.read(1024)
if not data:
break
f.write(data)
res.close()
except Exception as e:
print e
return False
return True