Exemplo n.º 1
0
  private InputStream getInputStream(boolean raw) throws Exception {
    if (raw) {
      return IOUtils.toInputStream(xmlEditorModel.getObject(), "utf-8");
    }
    File newFile = null;
    try {
      // Create new file
      MidPointApplication application = getMidpointApplication();
      WebApplicationConfiguration config = application.getWebApplicationConfiguration();
      File folder = new File(config.getImportFolder());
      if (!folder.exists() || !folder.isDirectory()) {
        folder.mkdir();
      }

      FileUpload uploadedFile = getUploadedFile();
      newFile = new File(folder, uploadedFile.getClientFileName());
      // Check new file, delete if it already exists
      if (newFile.exists()) {
        newFile.delete();
      }
      // Save file

      newFile.createNewFile();
      uploadedFile.writeTo(newFile);

      InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
      return new ReaderInputStream(reader, reader.getEncoding());
    } finally {
      if (newFile != null) {
        FileUtils.deleteQuietly(newFile);
      }
    }
  }
Exemplo n.º 2
0
  private void importReportFromFilePerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);

    FileUploadField file =
        (FileUploadField)
            get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
    final FileUpload uploadedFile = file.getFileUpload();
    if (uploadedFile == null) {
      error(getString("PageNewReport.message.nullFile"));
      target.add(getFeedbackPanel());

      return;
    }

    InputStream stream = null;
    File newFile = null;
    try {
      // Create new file
      MidPointApplication application = getMidpointApplication();
      WebApplicationConfiguration config = application.getWebApplicationConfiguration();
      File folder = new File(config.getImportFolder());
      if (!folder.exists() || !folder.isDirectory()) {
        folder.mkdir();
      }

      newFile = new File(folder, uploadedFile.getClientFileName());
      // Check new file, delete if it already exists
      if (newFile.exists()) {
        newFile.delete();
      }
      // Save file
      //            Task task = createSimpleTask(OPERATION_IMPORT_FILE);
      newFile.createNewFile();
      uploadedFile.writeTo(newFile);

      InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
      //            reader.
      stream = new ReaderInputStream(reader, reader.getEncoding());
      byte[] reportIn = IOUtils.toByteArray(stream);

      setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
    } catch (Exception ex) {
      result.recordFatalError("Couldn't import file.", ex);
      LoggingUtils.logException(LOGGER, "Couldn't import file", ex);
    } finally {
      if (stream != null) {
        IOUtils.closeQuietly(stream);
      }
      if (newFile != null) {
        FileUtils.deleteQuietly(newFile);
      }
    }

    showResult(result);
    target.add(getFeedbackPanel());
  }
Exemplo n.º 3
0
 public static Map<String, File> uploadFileFromUploadField(FileUpload fileUpload, int userId) {
   Application.debug("uploadFileFromUploadField", userId);
   Map<String, File> tmpDir = new HashMap<String, File>();
   String fileName = fileUpload.getClientFileName();
   UUID uuid = UUID.randomUUID();
   File dir = new File(Configuration.getTmp_dir() + "/" + uuid);
   if (!dir.mkdir()) {
     Application.error("cannot create directory uploadFileFromUploadField :" + uuid);
     return null;
   } else {
     String name = fileName;
     File tmpFile = new File(Configuration.getTmp_dir() + "/" + uuid + "/" + name);
     try {
       fileUpload.writeTo(tmpFile);
     } catch (IOException e) {
       Application.error(e);
       return null;
     }
     tmpDir.put(Configuration.getTmp_dir() + "/" + uuid, tmpFile);
     return tmpDir;
   }
 }
Exemplo n.º 4
0
    /** @see org.apache.wicket.markup.html.form.Form#onSubmit() */
    @Override
    protected void onSubmit() {
      final List<FileUpload> uploads = fileUploadField.getFileUploads();
      if (uploads != null) {
        for (FileUpload upload : uploads) {
          // Create a new file
          File newFile = new File(getUploadFolder(), upload.getClientFileName());

          // Check new file, delete if it already existed
          checkFileExists(newFile);
          try {
            // Save to new file
            newFile.createNewFile();
            upload.writeTo(newFile);

            UploadPage.this.info("saved file: " + upload.getClientFileName());
          } catch (Exception e) {
            throw new IllegalStateException("Unable to write file", e);
          }
        }
      }
    }
Exemplo n.º 5
0
 /**
  * Upload file from an FileUpload to the desired location with the desired name
  *
  * @param url - the URL
  * @param to - the output directory
  * @param name - the desired name of the file
  * @throws IOException
  */
 public static File uploadFileFromFileUpload(FileUpload fileUpload, String to, String name)
     throws IOException {
   File output = new File(to + "/" + name);
   fileUpload.writeTo(output);
   return output;
 }