Web Hyperlinks Download Resources

Source: Internet
Author: User

Some hyperlinks are available on the page, and there are resources in the corresponding server program, and when the hyperlinks are clicked, you can download the server Resource version 1: Respond the resource content to the browser
1 Create a resource under webcontent such as:
The first episode of Cucurbit doll 1 2 3 ABC
Beautiful. jpg
Aaa.txt Helloword Hello
2 Create a download.html under WebContent
<a href= "/day33_04/myservlet?filename=" > Gourd baby </a>
<a href= "/day33_04/myservlet?filename= beauty. jpg" > Beauty </a>
<a href= "/day33_04/myservlet?filename=aaa.txt" >aaa</a>
Note: Myservlet is the name of Url-prttern in Web. xml
3 Creating a Servlet class
Inherit HttpServlet
In the Doget method
1 Get the file name of the browser submission
String filename = request.getparameter ("filename");
2 File name garbled
filename = new String (filename.getbytes ("iso-8859-1"), "UTF-8");
3 read the corresponding resource through the file name, use ServletContext to get the input stream, and respond to the browser
ServletContext boss = Getservletcontext ();
InputStream is = Boss.getresourceasstream ("/" +filename);
4 response to browser via output stream
Servletoutputstream out = Response.getoutputstream ();
byte[] bytes = new byte[1024];
int Len;
while ((Len=is.read (bytes))!=-1) {
Sos.write (bytes, 0, Len);
}
5 Releasing Resources
Is.close ();

Steps:
To respond to a resource to a browser requires an IO stream using a byte input/output stream
Source files: Resources under WebContent may be transcoded in Chinese
Destination: Browser Response response
First get the source file
Web hyperlink link to resource get resource file name, Chinese transcoding
Read source file
Need Boss's getResourceAsStream
Write to Destination
Response Response Response Getoutputstream () required
Note the point:
Run-time Browser download.html version 2: Implement download, pop-up prompts customers to download processing
Before responding, tell the browser how to handle it----download
Response.setheader ("Content-disposition", "attachment; filename" +filename);
Version 3: Implement download, pop-up prompt customers to download processing
filename = new String (filename.getbytes ("iso-8859-1"), "UTF-8");
byte[] Bytes2 = filename.getbytes ("GBK");
String downloadfilename = new String (Bytes2, "iso-8859-1");
Steps
The resource should first respond to the browser and then download it from the browser to the client via popup window
Resource-->tomcat--> Browser Browser-->tomcat--> client
Resource to Browser Iso-8859-1-->utf-8
Browser-to-client encapsulation into GBK and then encapsulation into iso-8859-1
Peel the iso-8859-1 into GBK with Tomcat Note: If a resource is created to recreate a folder xx under WebContent, then version 1 of 3 InputStream is = Boss.getresourceasstream ("/" + filename); Change "/" to "/xx/" otherwise the null pointer exception is reported nullpointerexception

Web Hyperlinks Download Resources

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.