Friday, June 27, 2008

Play-mate Of The Apes Online Streaming

see

Here is an updated version of the java servlet code for "file upload".
will place the exact steps I followed for the example to work.

In principle, using Netbeans 6 and tomcat 6 for this example.

1. Obterner the library "Commons IO" (see 1.4) From

http://commons.apache.org/io/

2. Get the library "commons file upload" (in this case got the see 1.2.1 January 2008).

http://commons.apache.org/fileupload/ 3

. Both decompress in a folder "c: / libraries"

4. Create a new project called "FileUpload"


Note that I select "Tomcat 6" as the application server.




5. Create a new html called "UploadFile"






the new html Place the following code:





\u0026lt;HTML>



\u0026lt;HEAD>

\u0026lt;TITLE> \u0026lt;/ TITLE>

\u0026lt;/ HEAD>

\u0026lt;BODY>

\u0026lt;center>

\u0026lt;form method = "POST "enctype =" multipart / form-data '

action = "/ FileUpload / UploadFichero">

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>




6. Add the libraries to be decompressed in the file "c: / libraries
"


all libraries will be added commons file upload "

and all libraries will be added commons IO"




All libraries should be so in the environment:


7. It creates a new package, called org.txos



8. It creates a new servlet called "UploadFichero"




9. Place the following code in the servlet:





10. Make "Build" project code
/ * * UploadFichero.java





* * Created on 27/6/2008, 11:22:00 AM *



* To change this template, choose Tools

*/

package org.txos;

import java.io.*;

import java.net.*;

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 Os

*/

public class UploadFichero extends HttpServlet {

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

* @param request servlet request

* @param response servlet response

*/

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 saved in the address sgte

factory.setRepository (new File ("d :/"));

ServletFileUpload ServletFileUpload upload = new (factory);

/ / maximum allowable size (if it 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;}




@ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

processRequest (request, response);}



/ *

* Handles the HTTP POST method.

* @ param request servlet request * @ param

response

servlet response * /}








11. Run it ....

12. Jsp.index initially be displayed, change the path in the browser to:


13. You should see a page like this:


14. Find any file, preferably . Txt and upload ... will see the following page:

15. Find the file "upload" in c: / nuevo.txt.
16. End
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


0 comments:

Post a Comment