Read remote files with JSP, save to Local

Source: Internet
Author: User
Tags net 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{
Pick up the designated Web?? 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 Criteria"
</p>
<p>
<input name= "Maxresults" size= "4" value= "M"/> Results per Page
<input type= "Submit" value= "Search"/>
</p>
</form>
</center>
which
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 set up a firewall, you need to add, if it is directly connected to the Internet, you do not need.



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.