急,不用到SERVLET,只是给你一个URL地址,可以下载文件到本地吗?
比如说
http://xsc.jnu.edu.cn/jy/download/广州市公安局招考人民警察职位表.doc
我想把这个文件在JAVA中下载,但这里没有涉及到SERVLET,怎样才能下载到本地呢?万分致谢
------解决方案--------------------import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class A {
public static void main(String[] args) {
try {
URL url = new URL( "http://127.0.0.1:8080/vml/c.doc ");
URLConnection conn = url.openConnection();
InputStream fis = conn.getInputStream();
byte[] b = getBytesByInputStream(fis);
FileOutputStream fo = new FileOutputStream( "c:/c.doc ");
fo.write(b);
fo.flush();
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] getBytesByInputStream(InputStream fin) throws Exception {
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int nc = 0;
while ((nc = fin.read(buf, 0, buf.length)) > 0)
bStream.write(buf, 0, nc);
byte[] ret = bStream.toByteArray();
bStream.flush();
bStream.close();
return ret;
}
}