Download tutorial for Java files (not parsed)

Source: Internet
Author: User
Tags file copy
Let's start with a recent servlet study. The servlet is a piece of code that executes on the server.

Servlet has three canonical servlet technology, listener technology-listener, Fileter technology-filter. The latter two will be introduced later in the blog. In terms of what is a ServletContext object. The ServletContext object represents a Web application environment (context), and the ServletContext object has only one Web application.

ServletContext is a domain object--The domain object that is stored in the data region is scoped to all Web resources.

To say how to download the file, if you put the file on the server (the published website of the computer).

That's it, but you'll meet the situation. Want to download the JPG format, but the browser opened directly, no download, but the ZIP format can be downloaded. This will need to be set by the code to not allow the browser to parse. This one is available for download.

Create a new servlet file and write it in the get code.

Solve the garbled filename=new String (filename.getbytes ("iso8859-1"), "UTF-8") to get the Chinese parameter;// The type of file to download--The client differentiates the type Response.setcontenttype (This.getservletcontext (). GetMimeType () by the MIME type of the file;// Tells the client that the file is not parsed directly but opens (downloaded) response.setheader ("Content-disposition", "Attachment;filename=" +filename) in the form of an attachment;// The absolute path of the harvested file is string path = This.getservletcontext (). Getrealpath ("download/" +filename);//Get file input stream FileInputStream in = new FileInputStream (path);//Get output stream--response the output stream is used to write content to the client servletoutputstream out = Response.getoutputstream ();// File copy template code int len=0;byte[] Buffer=new byte[1024];while ((len=in.read (buffer)) >0) {out.write (buffer, 0, Len);} In.close (); Out.close ();

This out can be closed or off, but in needs to be closed because this is new in the program. Out will automatically close after running the program.

That's the way it is.

This is equivalent to the resources in the server.

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.