package wangluobiancheng.lesson04;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("http://localhost:8080/Alan/Securityfile.txt");
//2.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("Securityfile.txt");
byte[]buffer=new byte[1024];
int len;
while((len=inputStream.read())!=-1){
fos.write(buffer,0,len); //写出这个数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
打开就是这样的
我是直接在前端用window.open(文件地址),浏览器会自动下载,比较省事。