如何用java获取网络文件的大小
1个回答
2016-11-20 · 知道合伙人互联网行家
关注
展开全部
import java.net.*;
import java.io.*;
public class URLConnectionDemo{
public static void main(String[] args)throws Exception{
URL url = new URL("http://www.scp.edu.cn/pantoschoolzz/BG/Bord/Message/DownloadMessageAttachment.aspx?ID=215");
URLConnection uc = url.openConnection();
String fileName = uc.getHeaderField(6);
fileName = URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
System.out.println("文件名为:"+fileName);
System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
String path = "D:"+File.separator+fileName;
FileOutputStream os = new FileOutputStream(path);
InputStream is = uc.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
System.out.println("下载成功,文件保存在:"+path);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |