2012-08-30 11 views
5

en una aplicación web gwt. Tengo que enviar un archivo y algunos parámetros adjuntos.

en ServerSide

try { 

     ServletFileUpload upload = new ServletFileUpload(); 

     FileItemIterator iterator = upload.getItemIterator(request); 

     while (iterator.hasNext()) { 
      FileItemStream item = iterator.next(); 


      if (item.isFormField()) { 

       String fieldName=item.getFieldName(); 
       String fieldValue = Streams.asString(item.openStream()); 
       System.out.println(" chk " +fieldName +" = "+ fieldValue); 
      } else { 
       stream = item.openStream(); 
       fileName = item.getName(); 
       mimetype = item.getContentType(); 
       int c; 
       while ((c = stream.read()) != -1) { 
        System.out.print((char) c); 
        } 
      } 
     } 
    }catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
    System.out.println("out of try"); 
    ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    int nRead; 
    while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) { 
     System.out.println("lenth111" +nRead); 
     output.write(buffer, 0, nRead); 
    } 
    System.out.println("lenth" +nRead); 
    output.flush(); 

con este código que puedo leer la secuencia. y también en la consola "de prueba" también se imprime

Y por último en la línea while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) tengo una advertencia

ADVERTENCIA:/UploadFileServlet: org.apache.commons.fileupload.FileItemStream $ ItemSkippedException.

Cómo solucionar este problema. ??

+0

Hola GameBuilder y Thomas Broyer, ¿Le ha resultado una solución al respecto? Porque he estado teniendo el mismo problema por días. Por favor, ¿alguien podría ayudarnos? Gracias – Aerox

+0

hey, iam también estoy buscando lo mismo. ¿Obtuviste alguna solución? –

Respuesta

4

Responde un poco tarde pero tuve el mismo problema.

¿Por qué sacan esa excepción: Los JavaDocs de ItemSkippedException explican un poco:

Esta excepción se produce, si se hace un intento de leer los datos desde el InputStream, que ha sido devuelta por FileItemStream.openStream (), después de invocar Iterator.hasNext() en el iterador, que creó el FileItemStream.

Está utilizando la corriente InputStream fuera del bucle while que causa el problema porque otra iteración se llama que cierra (salta) El archivo InputStream intenta leer.

Solución: Utilice InputStream dentro del ciclo while. Si necesita todos los campos de formulario antes de procesar el archivo, asegúrese de configurarlo en el orden correcto en el lado del cliente. Primero todos los campos, el último archivo. Por ejemplo, usando el código JavaScript FormData:

var fd = new window.FormData(); 

fd.append("param1", param1); 
fd.append("param2", param2); 

// file must be last parameter to append 
fd.append("file", file); 

Y en el lado del servidor:

FileItemIterator iter = upload.getItemIterator(request); 
while (iter.hasNext()) { 
    FileItemStream item = iter.next(); 
    InputStream stream = item.openStream(); 

    // the order of items is given by client, first form-fields, last file stream 
    if (item.isFormField()) { 
     String name = item.getFieldName(); 
     String value = Streams.asString(stream); 
     // here we get the param1 and param2 
    } else { 
     String filename = item.getName(); 
     String mimetype = item.getContentType(); 

     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     int nRead; 
     while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) { 
      System.out.println("lenth111" +nRead); 
      output.write(buffer, 0, nRead); 
     } 
     System.out.println("lenth" +nRead); 
     output.flush(); 
    } 
} 
+0

Correcto, desearía poder sacarlo fuera del ciclo del iterador. –

Cuestiones relacionadas