Use JSP to read remote files and save them to a local device

Source: Internet
Author: User

Use JSP to read remote files and save them to a local device

Reading network files is somewhat different. I will give you a complete code and save it as JSP to run it directly.
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "java.net. *" %>
<% @ Page import = "Java. util. properties" %>
<%

//? 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 to the specified network ?? Source ,? Network fetch ?? Stream entry
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>
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 );
If your machine has set a firewall, you need to add it. If you directly connect to the Internet, you don't 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.