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()); }
public ImageUploadContentPanel(String pId, String customUploadFolderPath) { super(pId); setOutputMarkupId(true); this.uploadFolderPath = customUploadFolderPath; Form<?> form = new Form<Void>("form"); final FeedbackPanel feedback = new FeedbackPanel("feedback"); feedback.setOutputMarkupId(true); form.add(feedback); final FileUploadField fileUploadField = new FileUploadField("file"); fileUploadField.setLabel(new ResourceModel("required.label")); fileUploadField.setRequired(true); fileUploadField.add(FILE_EXTENSION_VALIDATOR); form.add(fileUploadField); form.add( new AjaxButton("uploadButton", form) { private static final long serialVersionUID = 1L; @Override protected void onSubmit(AjaxRequestTarget pTarget, Form<?> pForm) { FileUpload fileUpload = fileUploadField.getFileUpload(); String fileName = fileUpload.getClientFileName(); try { File currentEngineerDir = new File(uploadFolderPath); if (!currentEngineerDir.exists()) { currentEngineerDir.mkdir(); } fileUpload.writeTo(new File(currentEngineerDir, fileName)); } catch (Exception ex) { log.error("Can't upload attachment: " + ex.getMessage(), ex); ImageUploadContentPanel.this.error("Can't upload attachment"); pTarget.add(feedback); return; } finally { fileUpload.closeStreams(); } ImageFileDescription imageFileDescription = new ImageFileDescription(fileName); imageFileDescription.setContentType(fileUpload.getContentType()); onImageUploaded(imageFileDescription, pTarget); } @Override protected void onError(AjaxRequestTarget pTarget, Form<?> pForm) { pTarget.add(feedback); } }); add(form); }
public UploadableProfileImagePanel(String id, final long userId) { super(id, userId); final Form<Void> form = new Form<Void>("form"); form.setMultiPart(true); form.setMaxSize(Bytes.bytes(getBean(ConfigurationDao.class).getMaxUploadSize())); // Model is necessary here to avoid writing image to the User object form.add( fileUploadField = new FileUploadField( "image", new IModel<List<FileUpload>>() { private static final long serialVersionUID = 1L; // FIXME this need to be eliminated public void detach() {} public void setObject(List<FileUpload> object) {} public List<FileUpload> getObject() { return null; } })); form.add(new UploadProgressBar("progress", form, fileUploadField)); fileUploadField.add( new AjaxFormSubmitBehavior(form, "onchange") { private static final long serialVersionUID = 2160216679027859231L; @Override protected void onSubmit(AjaxRequestTarget target) { FileUpload fu = fileUploadField.getFileUpload(); if (fu != null) { StoredFile sf = new StoredFile(fu.getClientFileName()); if (sf.isImage()) { boolean asIs = sf.isAsIs(); try { // FIXME need to work with InputStream !!! getBean(GenerateImage.class) .convertImageUserProfile(fu.writeToTempFile(), userId, asIs); } catch (Exception e) { // TODO display error e.printStackTrace(); } } else { // TODO display error } } target.add(profile, form); } }); add(form.setOutputMarkupId(true)); add(BootstrapFileUploadBehavior.INSTANCE); }
@SuppressWarnings("unchecked") private void setSelectedCustomFieldsFromFile() { if (fileUploadField.getFileUpload() != null) { ArkFunction arkFunction = iArkCommonService.getArkFunctionByName( au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION); Long studyId = (Long) SecurityUtils.getSubject() .getSession() .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); Study study = iArkCommonService.getStudy(studyId); ArrayList<CustomField> selectedCustomFields = (ArrayList<CustomField>) iArkCommonService.matchCustomFieldsFromInputFile( fileUploadField.getFileUpload(), study, arkFunction); cpModel.getObject().setSelectedCustomFields(selectedCustomFields); } initCustomFieldPalette(); arkCrudContainerVO.getDetailPanelFormContainer().addOrReplace(customFieldPalette); }
@Override protected FormComponentLabel addComponentForRegular() { final FileUploadField fileUploadField = createFileUploadField(ID_SCALAR_VALUE); fileUploadField.setLabel(Model.of(getModel().getName())); final FormComponentLabel scalarIfRegular = new FormComponentLabel(ID_SCALAR_IF_REGULAR, fileUploadField); scalarIfRegular.add(fileUploadField); final Label scalarUploadLabel = new Label(ID_SCALAR_IF_REGULAR_UPLOAD, "Upload"); scalarIfRegular.add(scalarUploadLabel); final Label scalarName = new Label(ID_SCALAR_NAME, getModel().getName()); scalarIfRegular.add(scalarName); updateDownloadLink(ID_SCALAR_IF_REGULAR_DOWNLOAD, scalarIfRegular); scalarIfRegular.addOrReplace(new ComponentFeedbackPanel(ID_FEEDBACK, fileUploadField)); addOrReplace(scalarIfRegular); return scalarIfRegular; }
/** @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); } } } }
/** @see Form#onSubmit() */ @Override protected void onSubmit() { final String project = identifier.getModel().getObject(); try { final FileUpload upload = file.getFileUpload(); if (upload != null) { File dir = projects.reserveResourceId(project); // File target = new File(dir, upload.getClientFileName()); String type = upload.getContentType(); if (checkContentType(type)) { // IOUtils.copy(upload.getInputStream(), new FileOutputStream(target)); // try extracting the archive IOUtils.extract(dir, new BufferedInputStream(upload.getInputStream())); // trigger scan after upload projects.triggerScan(); info("Successfully uploaded project"); onUploadSuccess(); } else { projects.releaseResourceId(project); error(getTypeErrorMessage(type)); } } else { warn("Please provide a file for upload"); } } catch (ScavengerException e) { error(e.getMessage()); } catch (Exception e) { projects.releaseResourceId(project); log.error("Error while uploading file", e); error("Error saving the file"); } }
private FileUpload getUploadedFile() { FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT)); return file.getFileUpload(); }
public UserPicturePanel(final String id, final User usr) { super(id); this.user = usr; // Form for uploading profile images fileUploadForm = new Form<>("fileUploadForm"); fileUploadForm.setOutputMarkupId(true); // User image userPhoto = new NonCachingImage("userImage", user.getProfilePicture(User.PictureDimensions.Large)); userPhoto.setOutputMarkupId(true); fileUploadForm.add(userPhoto); uploadField = new FileUploadField("fileUploadField"); fileUploadForm.add(uploadField.setOutputMarkupId(true)); fileUploadForm.setMaxSize(Bytes.kilobytes(MAX_SIZE)); fileUploadForm.add(new Label("max", Model.of(Bytes.kilobytes(MAX_SIZE)))); fileUploadForm.add( new AjaxSubmitLink("submit") { private static final long serialVersionUID = 1L; @Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { // Check whether an image has been provided to be uploaded FileUpload fileUpload = uploadField.getFileUpload(); if (fileUpload != null) { try { uploadHelper.processUserFileUpload(fileUpload, user); userPhoto.setImageResource(user.getProfilePicture(User.PictureDimensions.Large)); uploadField.clearInput(); target.add(uploadField); target.add(userPhoto); } catch (IOException e) { getPage().error(e.getLocalizedMessage()); target.add(((BasePage) getPage()).getFeedbackPanel()); } } } }); fileUploadForm.add( new AjaxSubmitLink("clear") { @Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { uploadHelper.removeUserPictures(user); target.add(userPhoto); } }); fileUploadForm.add(new UploadProgressBar("progress", fileUploadForm, uploadField)); add(fileUploadForm); fileUploadForm.add( new AjaxSubmitLink("uploadGravatar") { /** */ private static final long serialVersionUID = 1L; @Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { // Attempt to download image from gravatar try { byte[] rawImage = gv.download(user.getMail()); if (rawImage != null) { uploadHelper.processUserFileUpload(rawImage, user); userPhoto.setImageResource(user.getProfilePicture(User.PictureDimensions.Large)); uploadField.clearInput(); target.add(uploadField); target.add(userPhoto); } } catch (IOException e) { uploadHelper.removeUserPictures(user); target.add(userPhoto); } } }); }