Friday, January 12, 2007

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

Mountains By Biffy Clyro Piano Scores

Servlet File Download File Upload Servlet

Here I go a very simple piece of code that serves to raise (to upload) a file from the client to the server machine. I hope they serve.

Steps:


1. It must obtain the following jars (download them from jakarta site) - commons-fileupload-1.1.1.jar
- commons-io-1.2.jar


2. Unzip the jars and place them in:
... webapps \\ project name
\\ WEB-INF \\ lib This code containing the jars, will be made available for your project.


Eye: project name refers to the name "You" put to your project (located in "webapps").
If for example this was called "test1", then the path would be as follows: ... webapps \\ test1
\\ WEB-INF \\ lib



3. Create in: ... webapps \\
project name \\ jsp \\ examples
the following HTML page, under the name of "uploadFile.html"




\u0026lt;HTML>
\u0026lt;HEAD>

\u0026lt;TITLE> \u0026lt;/ TITLE>

\u0026lt;/ HEAD>

\u0026lt;BODY>

\u0026lt;center>
\u0026lt;form method = "POST" enctype = 'multipart / form-data'
action = "/ bexsys / servlet / ejemplos.UpFichero">

Select file to send to the server \u0026lt;br>
\u0026lt;input type = "file"
name = "file "> \u0026lt;input type="submit"
value="Upload">

\u0026lt;/ form>

\u0026lt;/ center>

\u0026lt;/ BODY>

\u0026lt;/ HTML>






4. Now create the servlet: Believe in the following directory: ... webapps \\

name project \\ WEB-INF \\ classes \\ examples under the name "UploadFichero.java"



package examples;

/* * UploadFichero.java *
* Created on January 12, 2007, 9:00 AM
*/


import java.io.*; import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.util.*;

/*
*
* @author Oscar Hurtado Morato
*
*
*/

public class UpFichero extends HttpServlet {


protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {boolean ok =
getNSaveFile (request);
response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
out.println

(""); out.println (""); out.println (""); out.println ("");


out.println (""); if (ok) {
out. println ("The file transfer was successful.");}


else {out.println ("file transfer failed.");}


out.println (""); out.println (" ");
out.close ();}

Public boolean
getNSaveFile (HttpServletRequest request) {try {

/ / Check that it is a multipart request (which is this sending a file) boolean
isMultipart = ServletFileUpload.isMultipartContent (request);
System.out.println ("Is multipart = "+ isMultipart) DiskFileItemFactory
DiskFileItemFactory factory = new ();
/ / maximum size that will be stored in memory
factory.setSizeThreshold (4096);
/ / if you exceed the previous size, it will go away temporarily keeping in the direction sgte
factory.setRepository (new File ("d :/")); ServletFileUpload
ServletFileUpload upload = new (factory);
/ / maximum allowable size (if jumps to the catch exceeds)
upload.setSizeMax (10000000);
List fileItems = upload.parseRequest (request);
/ / get the file sent fileItems.iterator
Iterator i = ();
FileItem fi = (FileItem) i. next ();
/ / saves the file sent to the local server
/ / path and file name of the destination (the server)
String path = "c: /";
String fileName = "nuevo.txt"
fi.write (new File (path, fileName));

} catch (Exception e) {
System.out.println (e.getMessage ());
e.printStackTrace ();
return false;}

return true;}


/ * * Handles the HTTP


POST method. * @ param request servlet request * @ param response
servlet response * / protected void
doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest (request, response);
}}




5. Note that the file uploaded, will be saved on the hard "c: \\" under the name "test.txt". 6. End