试试:public static void main(String [] args){
try {
URL url = new URL("http://army.news.tom.com/script/midi/003.mp3");
InputStream ins = url.openStream();
writeFile(new File("c:/abcdefg.mp3"), ins);
System.out.println("down");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeFile( File saveToFile, InputStream ins) throws IOException{
OutputStream bos = new FileOutputStream(saveToFile, false);
int bytesRead = 0;
int c = 0;
float k = 0;
float m = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = ins.read(buffer, 0, 1024)) != -1){
bos.write(buffer, 0, bytesRead);
c+=bytesRead;
k = c/1024;
m = k/1024;
System.out.println("己下载: " + c+" b" + k+"k " + m+"M");
}
bos.close();
}
try {
URL url = new URL("http://army.news.tom.com/script/midi/003.mp3");
InputStream ins = url.openStream();
writeFile(new File("c:/abcdefg.mp3"), ins);
System.out.println("down");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeFile( File saveToFile, InputStream ins) throws IOException{
OutputStream bos = new FileOutputStream(saveToFile, false);
int bytesRead = 0;
int c = 0;
float k = 0;
float m = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = ins.read(buffer, 0, 1024)) != -1){
bos.write(buffer, 0, bytesRead);
c+=bytesRead;
k = c/1024;
m = k/1024;
System.out.println("己下载: " + c+" b" + k+"k " + m+"M");
}
bos.close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货