首页 > 编程 > JSP > 正文

用jsp读远程文件,保存到本地

2024-09-05 00:19:59
字体:
来源:转载
供稿:网友

用jsp,读远程文件,保存到本地

读取网络文件有些不一样,我给你一个完整的代码吧,存成jsp就可以直接运行的。
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.properties"%>
<%

//?程文件路径
string s1 = "http://www.google.co.jp";
//本地存放路径
string s2 = "c://test.html";

url urlfile = null;
httpurlconnection httpurl = null;
bufferedinputstream bis = null;
bufferedoutputstream bos = null;
file f = new file(s2);

//make proxy
string proxy = "192.168.224.12";
string port = "8080";
properties systemproperties = system.getproperties();
systemproperties.setproperty("http.proxyhost",proxy);
systemproperties.setproperty("http.proxyport",port);

try{
//?接指定的网??源,?取网??入流
urlfile = new url(s1);
httpurl = (httpurlconnection)urlfile.openconnection();
httpurl.connect();
bis = new bufferedinputstream(httpurl.getinputstream());
}catch(exception e){
system.out.println(e.tostring());
}

try{
bos = new bufferedoutputstream(new fileoutputstream(f));;
byte[] b = new byte[1024];
while(bis.read(b)!=-1) {
bos.write(b);

}catch(exception e){
system.out.println(e.tostring()); 
}finally{
try{
bos.flush();
bis.close();
httpurl.disconnect();
}catch(exception e){
system.out.println(e.tostring()); 
}
}
 
%>
<center>
<form name="search" action="results.jsp" method="get">
<p>
<input name="query" size="44"/>&nbsp;search criteria
</p>
<p>
<input name="maxresults" size="4" value="100"/>&nbsp;results per page&nbsp;
<input type="submit" value="search"/>
</p>
        </form>
</center>
其中
//make proxy
string proxy = "192.168.224.12";//防火墙地址
string port = "8080"; //防火墙端口
properties systemproperties = system.getproperties();
systemproperties.setproperty("http.proxyhost",proxy);
systemproperties.setproperty("http.proxyport",port);
这一段是如果你的机器设定了防火墙,需要加上,如果是直接连上网,就不用。

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表