Rapadura What Can You Sub
code Here I go to "dowload" of a file that is on the server to the client.
hope and serve them.
1. Create an html page with the name of
"download. html "
, in the following directory .. / webapps / project name
/ jsp / examples Eye:
project name refers to the name "You" put your project (located in "webapps"). If, for example, this was called "test1", then the path would be as follows: ... webapps \\ test1 \\ jsp \\ examples Containing the following code:
\u0026lt;% @ page contentType = "text / html"% > \u0026lt;html> \u0026lt;head> \u0026lt;title> jspQuery \u0026lt;/ title> \u0026lt;/ head> <body> <form action="/nombre proyecto/servlet/ejemplos.Download"> <p><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong> </strong></font></p> <p> <input type="submit" value="Download" name="dato" > </p> <p> </p> <p> </p> \u0026lt;/ Form> \u0026lt;/ body> \u0026lt;/ html> |
2. Create a file under the name:
"Download.java"
at the following address: \\ webapps \\
name project \\ WEB-INF \\ classes \\ examples contain the following code:
/ * * Download.java * * Created on Jan 12, 2006, 04:00 PM */ package ejemplos; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author Oscar Hurtado Morato * @version 1.0 */ public class DownloadError extends HttpServlet { /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); } /** Destroys the servlet. */ public void destroy() { } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { //Aqui placed the path and name of the file to be upload String filePath = "c: /"; String fileName = "test.txt"; fileToDownload = new FileInputStream FileInputStream (filePath); response.getOutputStream ServletOutputStream out = ( ) response.setContentType ("application / zip"); response.setHeader ("Content-Disposition", "attachment; filename =" + fileName); response.setContentLength (fileToDownload.available ()); int c ; while ((c = fileToDownload.read ())! = -1) { out.write (c);} out.flush (); out.close (); fileToDownload.close (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException , java.io.IOException {processRequest (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response ) throws ServletException, java.io.IOException {processRequest ( request, response); }} |
3. End
0 comments:
Post a Comment