Use JSP, read remote file, save to Local

Source: Internet
Author: User
Tags connect tostring firewall
JS

Use JSP, read remote file, save to Local

Read the network file some different, I give you a complete code, stored as a JSP can be directly run.
<%@ page import= "java.io.*"%>
<%@ page import= "java.net.*"%>
<%@ page import= "Java.util.Properties"%>
<%

Process file path
String S1 = "http://www.google.co.jp";
Local Storage Path
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{
//? Connect the specified network?? SOURCE, net?? Inflow
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= " /> search the Criteria
</p>
<p>
<input name= "Maxresults" size= "4" value= "/>"  results per page 
<input type= "Submit" value= "Search"/>
</p>
         </form>
</center>
where
//make proxy
String proxy = "192.168.224.12" //Firewall address
String port = "8080";//Firewall port
Properties systemproperties = system.getproperties ();
systemproperties.setproperty ("Http.proxyhost", proxy);
Systemproperties.setproperty ("Http.proxyport", port);
This section is if your machine has a firewall set up, you need to add, if it is directly connected to the Internet, do not.








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.