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.