java 下载历程

做了一个社团分配的任务,还算不错,能够部分实现了它的功能了,但主要的界面设计。。。哎,头疼,算啦,慢慢来吧,总会把他搞定的!!


部分代码(无界面,但实现了下载功能):

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URISyntaxException;

import java.net.URL;

import java.net.URLConnection;

import java.util.Scanner;

//import java.awt.Desktop;

public class test_url {

private static URL url;

//URL u=this.getDocumentBase();

public static void main(String args[])throws IOException, URISyntaxException{

test_url s=new test_url();

Scanner sc =new Scanner(System.in);

System.out.println("请输入网址:");

String s2=sc.next();

String url="http://"+s2;

try {

URL U=new URL(url);

URLConnection c=U.openConnection();

InputStream file=c.getInputStream();

String fname = U.getFile().substring(U.getFile().lastIndexOf("/") + 1);

String xxx=fname.substring(fname .lastIndexOf(".")+1);

System.out.println("输入文件名:");

String s1=sc.next();

FileOutputStream out=new FileOutputStream("G:/"+s1+"."+xxx);

System.out.println("开始下载");

int k;

while((k=file.read())>=0){

         run(k);

out.write(k);

out.flush();

}

out.close();

file.close();

System.out.println("下载完成");

 

//java.awt.Desktop.getDesktop().browse(new URL(url).toURI());

/*HttpURLConnection h=(HttpURLConnection)(U.openConnection());

int num=h.getResponseCode();

if(num>400){

System.out.println("错误!");

}*/

//new URL(url).toURI();

} catch (MalformedURLException e) {

e.printStackTrace();

}

/*File f=new File(s);

FileInputStream file=new FileInputStream(f);

FileOutputStream file2=new FileOutputStream("G:/");

int num;

while((num=file.read())!=-1){

file2.write(num);

}

System.out.println("文件保存成功!");*/

}

 

public static void run(int m) {

try{

while(m>=0){

Thread.sleep(1000);

}

}catch(Exception e){

e.printStackTrace();

}

run(m);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值