Exemplo n.º 1
0
 void handleFileUpload(HttpServletRequest request, HttpServletResponse response)
     throws IOException {
   try {
     ServletFileUpload upload = createUpload();
     FileItemIterator iter = upload.getItemIterator(request);
     while (iter.hasNext()) {
       FileItemStream item = iter.next();
       if (!item.isFormField()) {
         receive(item);
       }
     }
     if (tracker.isEmpty()) {
       String errorMessage = "No file upload data found in request";
       tracker.setException(new Exception(errorMessage));
       tracker.handleFailed();
       response.sendError(HttpServletResponse.SC_BAD_REQUEST, errorMessage);
     } else {
       tracker.handleFinished();
     }
   } catch (Exception exception) {
     Throwable cause = exception.getCause();
     if (cause instanceof FileSizeLimitExceededException) {
       exception = (Exception) cause;
     }
     tracker.setException(exception);
     tracker.handleFailed();
     int errorCode =
         exception instanceof FileSizeLimitExceededException
             ? HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE
             : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
     response.sendError(errorCode, exception.getMessage());
   }
 }
Exemplo n.º 2
0
 private void receive(FileItemStream item) throws IOException {
   InputStream stream = item.openStream();
   try {
     String fileName = stripFileName(item.getName());
     String contentType = item.getContentType();
     FileDetails details = new FileDetailsImpl(fileName, contentType);
     FileUploadReceiver receiver = handler.getReceiver();
     receiver.receive(stream, details);
     tracker.addFile(details);
   } finally {
     stream.close();
   }
 }