1. the most direct and simple way to download JSP files is to put the file address directly into a link on the html page. The disadvantage is that the file path on the server is exposed, and the file download cannot be controlled, such as permissions ). This will not be used as an example.
2. on the server side, convert the file into an output stream, write it to response, and take the file to the browser. The browser will prompt you whether you want to save the file locally. JSP File Download example)
- <%
- Response. setContentType (fileminitype );
- Response. setHeader ("Location", filename );
- Response. setHeader ("Cache-Control ","Max-age= "+ CacheTime );
- Response. setHeader ("Content-Disposition", "attachment;Filename= "+ Filename); // filename should be encoded (UTF-8)
- Response. setContentLength (filelength );
- OutputStreamOutputStream=Response. GetOutputStream ();
- InputStreamInputStream=NewFileInputStream (filepath );
- Byte []Buffer=NewByte [2, 1024];
- IntI=-1;
- While ((I=InputStream. Read (buffer ))! =-1 ){
- OutputStream. write (buffer, 0, I );
- }
- OutputStream. flush ();
- OutputStream. close ();
- InputStream. close ();
- OutputStream=Null;
-
- %>
3. Since it is a JSP file download, there is also a JSP file download method that uses Applet to download files. However, the customer must first trust your Applet to accept the data streams sent by the servlet and write them to the local machine.
Servlet example
- Public void service (HttpServletRequest req, HttpServletResponse res)
- Throws ServletException, IOException {
- Res. setContentType ("text/plain ");
- OutputStreamOutputStream=Null;
- Try {
- OutputStream=Res. GetOutputStream ();
- PopFile (srcFile, outputStream); // write the file path srcFile to outputStream.
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
JApplet sample
URLConnection con;
- Analysis on CGI supported by jsp http Server
- Use JSP pages to generate PDF reports
- Step for customizing JSP labels
- Detailed test of JSP containers
- Describes the following features of jsp http Server