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); } } }
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()); }