Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package SubirArchivoJSP; import com.sun.xml.ws.transport.tcp.server.glassfish.ServletFakeArtifactSet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * * @author gonza */ @WebServlet(name = "subirarchivojsp", urlPatterns = { "/subirarchivojsp" }) public class subirarchivojsp extends HttpServlet { private String dir, nombre; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ /* TODO output your page here. You may use following sample code. */ dir = getServletContext().getRealPath("/SubirArchivoJSP/img"); HttpServletRequest respuesta = request; if (ServletFileUpload.isMultipartContent(request)) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(5000 * 1024); // bytes List listUploadFiles = null; FileItem item = null; try { listUploadFiles = upload.parseRequest(request); Iterator it = listUploadFiles.iterator(); while (it.hasNext()) { item = (FileItem) it.next(); if (!item.isFormField()) { if (item.getSize() > 0) { nombre = item.getName(); File archivo = new File(dir, nombre); item.write(archivo); if (archivo.exists()) { out.println("GUARDADO " + archivo.getAbsolutePath() + "</p>"); } else { // nunca se llega a ejecutar out.println( "FALLO AL GUARDAR. NO EXISTE " + archivo.getAbsolutePath() + "</p>"); } } } else { // es un campo de formulario, podemos obtener clave y valor String key = item.getFieldName(); String valor = item.getString(); respuesta.setAttribute(key, valor); } } } catch (FileUploadException e) { out.println("Demasiado grande! <br>"); e.printStackTrace(); } catch (Exception e) { out.println("Ha ocurrido un error! <br>"); e.printStackTrace(); } } respuesta.setAttribute("foto", "/Servlet1/SubirArchivoJSP/img/" + nombre); out.print("hola"); RequestDispatcher vista = request.getServletContext() .getRequestDispatcher("/SubirArchivoJSP/vista.jsp"); vista.forward(respuesta, response); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }