1。直接用FILE类,但是有权限的问题。你要保证你登陆本地的帐户在远程电脑上也存在。
2。通过流传递过去,可以参考JAR包-FILEUPLOAD。
//:URLReader.java ==================import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.net.URL
public class URLReader {
public static void readAndSave(String url,String fileFullName)throws Exception{
System.out.println("Starting.")
URL u = new URL(url)
byte[] buffer = new byte[1024*8]
int read
int ava = 0
long start = System.currentTimeMillis()
BufferedInputStream bin = new BufferedInputStream(u.openStream())
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileFullName))
while((read=bin.read(buffer))>-1){
bout.write(buffer,0,read)
ava+=read
long speed = ava/(System.currentTimeMillis()-start)
System.out.println("Download: "+ava+" byte(s)"+"avg speed: "+speed+" (kb/s)")
}
bout.flush()
bout.close()
System.out.println("Done. size:"+ava+" byte(s)")
}
public static void main(String[] args) throws Exception {
URLReader.readAndSave(
"http://51hr.28tel.com.tw/UploadFile/Record/20090531114140.mp3",
"d:/双人舞.mp3")
}
}
======================================================
输出显示:
Starting.
Download: 8192 byte(s)avg speed: 6 (kb/s)
Download: 8385 byte(s)avg speed: 6 (kb/s)
Download: 16577 byte(s)avg speed: 11 (kb/s)
Download: 17025 byte(s)avg speed: 11 (kb/s)
Download: 25217 byte(s)avg speed: 15 (kb/s)
Download: 25665 byte(s)avg speed: 16 (kb/s)
Download: 33857 byte(s)avg speed: 19 (kb/s)
Download: 34305 byte(s)avg speed: 19 (kb/s)
1、远程读取数据的原则,是存在url可以指向该文件。2、很显然,你的局域网上,肯定是不行的。
3、方法:通过ftp或是发布到网上的方式,提供一个可访问到的网络地址就可以了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)