用jsp,讀遠程檔案,儲存到本地

來源:互聯網
上載者:User
js

用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"/> Search Criteria
</p>
<p>
<input name="maxresults" size="4" value="100"/> Results Per Page 
<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);
這一段是如果你的機器設定了防火牆,需要加上,如果是直接連上網,就不用。


 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.