Friday, January 12, 2007
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
-
2. Unzip the jars and place them in:
Eye:
If for example this was called "test1", then the path would be as follows: ... webapps \\ test1
\\ WEB-INF \\ lib
3. Create in: ... webapps \\
\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
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
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment