Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 45140 invoked from network); 15 Oct 2009 15:41:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Oct 2009 15:41:32 -0000 Received: (qmail 31738 invoked by uid 500); 15 Oct 2009 15:41:30 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 31651 invoked by uid 500); 15 Oct 2009 15:41:30 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 31641 invoked by uid 99); 15 Oct 2009 15:41:30 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Oct 2009 15:41:30 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [200.181.15.14] (HELO alpha2.planalto.gov.br) (200.181.15.14) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Oct 2009 15:41:21 +0000 Received: by alpha2.planalto.gov.br (Postfix, from userid 100) id 9F7B6D0836; Thu, 15 Oct 2009 12:31:39 -0300 (BRT) Received: from contas.in.planalto.gov.br (unknown [10.1.2.140]) by alpha2.planalto.gov.br (Postfix) with ESMTP id BFD82D0457 for ; Thu, 15 Oct 2009 12:31:36 -0300 (BRT) Received: by CONTAS with Internet Mail Service (5.5.2657.72) id <447KZDHC>; Thu, 15 Oct 2009 12:40:48 -0300 Message-ID: From: siomara@portosdobrasil.gov.br To: user@commons.apache.org Subject: RES: File Upload Date: Thu, 15 Oct 2009 12:40:25 -0300 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2657.72) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01CA4DAD.D0AB82E4" X-Virus-Checked: Checked by ClamAV on apache.org ------_=_NextPart_001_01CA4DAD.D0AB82E4 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Just an example that worked fine for me: /* * File Upload - Arquivo de licita=E7=F5es - October, 2009. */ package servlets.comum; import java.io.IOException; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import java.util.Iterator; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import javax.servlet.ServletException; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; //import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileItemFactory; //import org.apache.commons.fileupload.FileItemIterator; //import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; //import org.apache.commons.fileupload.util.Streams; import comum.Arquivo; import comum.ArquivoGestor; //import comum.TipoArquivo; import licitacao.Licitacao; import comum.Usuario; /** * * @author siomara */ public class MantemArquivo extends HttpServlet { =20 // Process the HTTP GET request protected void doGet(HttpServletRequest request, = HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }=20 protected void doPost(HttpServletRequest request, = HttpServletResponse response) throws ServletException, IOException { final int INSERT =3D 1; final int UPDATE =3D 2; final int DELETE =3D 3; // Retrieve operation to be performed int operation =3D Integer.parseInt(request.getParameter("op")); System.out.println("operation=3D"+operation); =20 // Create objects necessary to add/update/delete arquivo Arquivo arquivo =3D new Arquivo(); ArquivoGestor arquivoGestor =3D new ArquivoGestor(); =20 // Set session true HttpSession session =3D request.getSession(true); =20 Usuario usuarioAutenticado =3D (Usuario) session.getAttribute("usuarioAutenticado"); switch (operation) { case DELETE: =20 // Retrieve from sessiom 'arquivo' to be deleted arquivo =3D (Arquivo) session.getAttribute("arquivo"); // Delete 'arquivo' arquivoGestor.delete(arquivo); =20 // Release resources arquivoGestor.releaseResources(); =20 // Here the delete is complete. Send user to 'exibeLicitacaoAdmin.jsp' RequestDispatcher dispatcher3 =3D this.getServletContext().getRequestDispatcher("/licitacao/exibeLicitacao= Admi n.jsp"); dispatcher3.forward(request, response); =20 break; =20 default: =20 /** * Default will perfom INSERT and UPDATE because these operations have * lots of things in commom. However be alert to their = own particularities.=20 */ =20 // Create object to deal with dates DateFormat dateFormat =3D new = SimpleDateFormat("dd/MM/yyyy"); =20 // Retrieve licitacao from the session for UPDATE = operation only if (operation =3D=3D UPDATE) { arquivo =3D (Arquivo) = session.getAttribute("arquivo"); } =20 // Upload boolean isMultiPart =3D ServletFileUpload.isMultipartContent(request); if (isMultiPart) { FileItemFactory factory =3D new = DiskFileItemFactory(); ServletFileUpload upload =3D new ServletFileUpload(factory); String formulario =3D ""; try { List items =3D upload.parseRequest(request); Iterator iter =3D items.iterator(); while (iter.hasNext()) { FileItem item =3D (FileItem) iter.next(); if (item.getFieldName().equals("tipoForm")) = { formulario =3D item.getString(); } =20 If (item.getFieldName().equals("txtTituloApresentacao")) { =20 arquivo.setTituloApresentacao(item.getString()); } =20 if (item.getFieldName().equals("txtNomeInterno")) { = arquivo.setNomeInterno(item.getString()); } =20 if (item.getFieldName().equals("cboTipoArquivo")) { =20 arquivo.getTipoArquivo().setTipoArquivoId(Integer.parseInt(item.getStrin= g()) ); } =20 if (item.getFieldName().equals("txtDataPublicacaoDou")) { try { java.util.Date data =3D new java.sql.Date(dateFormat.parse(item.getString()).getTime()); arquivo.setDataPublicacaoDou(data); } catch (ParseException e) { e.printStackTrace(); } } if (!item.isFormField()) { if (item.getName().length() > 0) { this.inserirNoDiretorio(item); } } } }=20 catch (FileUploadException ex) { = ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } switch (operation) { case INSERT: =20 // Set primary key to zero. Sequence will be incremented during add method arquivo.setArquivoId(0); =20 // Set register ID. Date will be registered on = the add method. =20 arquivo.getRegistro().setIncluidoPorId(usuarioAutenticado.getUsuarioId()= ); =20 // Add new arquivo to the database arquivoGestor.add(arquivo); =20 Licitacao licitacao =3D new Licitacao(); licitacao =3D (Licitacao) session.getAttribute("licitacao"); arquivoGestor.linkArquivoTolLicitacao(arquivo, licitacao); =20 // Release resources arquivoGestor.releaseResources(); = =20 =20 // Here the registration is complete. Send user = to 'exibeArquivo.jsp' RequestDispatcher dispatcher =3D this.getServletContext().getRequestDispatcher("/licitacao/exibeLicitacao= Admi n.jsp"); dispatcher.forward(request, response); =20 break; =20 case UPDATE: =20 // Retrieve arquivo antigo from the session arquivo =3D (Arquivo) = session.getAttribute("arquivo"); =20 // Set register ID. Date will be registered on = the update method. =20 arquivo.getRegistro().setAlteradoPorId(usuarioAutenticado.getUsuarioId()= ); =20 // Update arquivo arquivoGestor.update(arquivo); =20 // Release resources arquivoGestor.releaseResources(); =20 // Here the update is complete. Send user to 'exibeLicitacao.jsp' RequestDispatcher dispatcher2 =3D this.getServletContext().getRequestDispatcher("/licitacao/exibeLicitacao= Admi n.jsp"); dispatcher2.forward(request, response); break; } break; } } /** *=20 * @param item FileItem, representa um arquivo que =E9 enviado pelo formulario * MultiPart/Form-data * @throws IOException */ private void inserirNoDiretorio(FileItem item) throws IOException { //Pega o diret=F3rio /logo dentro do diret=F3rio atual de onde = a aplica=E7=E3o est=E1 rodando //String caminho =3D getServletContext().getRealPath("/logo") + = "/"; String caminho =3D "c:/psplicitacao" + "/"; // Cria o diret=F3rio caso ele n=E3o exista File diretorio =3D new File(caminho); if (!diretorio.exists()) { diretorio.mkdir(); } =20 // Mandar o arquivo para o diret=F3rio informado String nome =3D item.getName(); String arq[] =3D nome.split("\\\\"); for (int i =3D 0; i < arq.length; i++) { nome =3D arq[i];=20 } File file =3D new File(diretorio, nome); FileOutputStream output =3D new FileOutputStream(file); InputStream is =3D item.getInputStream(); byte[] buffer =3D new byte[2048]; int nLidos; while ((nLidos =3D is.read(buffer)) >=3D 0) { output.write(buffer, 0, nLidos); } output.flush(); output.close(); } } -----Mensagem original----- De: jcarman@carmanconsulting.com [mailto:jcarman@carmanconsulting.com] = Em nome de James Carman Enviada em: quarta-feira, 14 de outubro de 2009 08:57 Para: Commons Users List Assunto: Re: File Upload The parameters from your form aren't sent as usual when you use multipart form data. They are sent as "parts." So, as you iterate through the FileItem objects from your upload, some of them will be form fields. You can check by calling isFormField(). You can call getFieldName() to get the field name and getString() to get the field's value. Check out this document for more information: http://commons.apache.org/fileupload/using.html On Wed, Oct 14, 2009 at 12:02 AM, Anand Shankar = wrote: > Hello > > I am facing more problem about File Upload and anybody is helping me. = I have > listed my problem on all the open source forum, so please give me = your some > important moment to suggest that how can I solve this problem. > > > I am using a form with enctype=3D"multipart/form-data" in order to = upload > files from the form. > I have read this page: = http://commons.apache.org/fileupload/using.html and > everything works well for my form. > > The only problem is that I can't get all the values from a "multiple select" > html object. I get only one value. > > Using servlets I have used this method: > > *public* java.lang.String[] getParameterValues(java.lang.String name) > > > > But now I have enctype=3D"multipart/form-data" in my form and I can't = use this > way... > Is there a way to get all the values of a multi-valued parameter? > > > Thanks a lot! > > -- > Anand Shankar > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org ------_=_NextPart_001_01CA4DAD.D0AB82E4--