2012-08-30 8 views
5

in un'applicazione Web gwt. Devo inviare un file e alcuni parametri ad esso allegati.

su 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 questo codice posso leggere il flusso. e anche su console "fuori provare" è stampato anche

E infine sulla while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) linea ho ottenuto un avvertimento

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

Come risolvere questo problema. ??

+0

Hi GameBuilder e Thomas Broyer, Hai trovato una soluzione su questo? Perché ho avuto lo stesso problema per giorni. Per favore, qualcuno potrebbe aiutarci? Grazie – Aerox

+0

hey, anche io sto cercando lo stesso. Hai trovato qualche soluzione? –

risposta

4

Risposta un po 'in ritardo ma ho avuto lo stesso problema.

Perché si ottiene tale eccezione: Le JavaDocs di ItemSkippedException spiegare un po ':

Questa eccezione viene generata, se viene effettuato un tentativo di leggere i dati dal InputStream, che è stato restituito da FileItemStream.openStream (), dopo che Iterator.hasNext() è stato richiamato sull'iteratore, che ha creato FileItemStream.

Si utilizza il flusso InputStream al di fuori del ciclo while che causa il problema, perché un altro iterazione si chiama che chiude (salta) Il file InputStream si tenta di leggere.

Soluzione: utilizzare InputStream all'interno del ciclo while. Se sono necessari tutti i campi modulo prima di elaborare il file, assicurarsi di impostarlo nell'ordine corretto sul lato client. Prima tutti i campi, ultimo il file. Ad esempio, utilizzando il formdata JavaScript:

var fd = new window.FormData(); 

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

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

E sul lato server:

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

Corretto, vorrei che fosse possibile portarlo fuori dal loop iteratore. –

Problemi correlati