永发信息网

java代码转换为python的代码,由于本人不会java,所以求指教,下面是一小段!

答案:1  悬赏:20  手机版
解决时间 2021-03-03 00:44
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
一言酒鼎在什么地方啊,我要过去处理事情
面包车电瓶没电了怎么办
一个物体从高空下落,已知第一秒钟下落的距离
后洋坪我想知道这个在什么地方
怎么快速记住administration 这个英语单词?
请问大神:活动板房搭建光做工不知多少钱一平
新郑卷烟厂家属院西门怎么去啊,有知道地址的
学校举办诗歌朗诵比赛,下面是朗诵目录的一部
下列各项中,能够计入成本的交易费用有(  
掏完耳朵耳朵里堵得慌是怎么回事,耳道疼?
繁体字当多少画
从上海戏剧学校毕业以后可以当什么样的演员呢
艾尚早餐怎么去啊,有知道地址的么
怎样才能清除在五八同城申请过的职位。
陶瓷贴片电容的最大工作频率是多少?
推荐资讯
做完深蹲以后,膝关节上方感觉火热,为什么(
运动鞋底边变黄该怎样刷白呢?
vivox5价格
昆山晶旭磁性材料厂地址在哪,我要去那里办事
洁美发型设计中心地址在什么地方,想过去办事
狼说喜欢吃讲话慢的小兔子,小兔子说哦酱紫啊
红云酒店地址有知道的么?有点事想过去
2011年上半年,我国钢铁产品市场需求旺盛,但
为什么我的电脑音响低音箱有时候嗡嗡响
毛戈平形象设计艺术学校在哪里啊,我有事要去
很色的动漫叫啥名?
理光C4502怎么限制彩色打印
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?