From e15ad6dc626c62062c6738efc82891df7f435898 Mon Sep 17 00:00:00 2001 From: Tim Holloway Date: Sat, 1 Jan 2022 14:09:25 -0500 Subject: [PATCH] Got fileupload working --- pom.xml | 27 ++++++++++++ .../mousetech/gourmetj/RecipeDetailBean.java | 14 +++--- .../gourmetj/SpringPrimeFacesApplication.java | 44 +++++++++++++++++++ .../gourmetj/springweb/PictureController.java | 8 ++-- .../META-INF/resources/detailEdit.xhtml | 41 ++++++++--------- 5 files changed, 102 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index 4879b76..502bc34 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,33 @@ spring-boot-starter-web + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.3 + provided + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + + + org.glassfish + javax.el + + + + commons-fileupload + commons-fileupload + 1.3 + + + commons-io + commons-io + 2.2 + + org.xerial sqlite-jdbc diff --git a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java index ec467af..b62a874 100644 --- a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java +++ b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java @@ -17,6 +17,8 @@ import javax.inject.Inject; import javax.inject.Named; import javax.servlet.http.Part; +import org.primefaces.event.FileUploadEvent; +import org.primefaces.model.UploadedFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -888,13 +890,13 @@ public class RecipeDetailBean implements Serializable { /** * Load/replace images. Computes thumbnail. * - * @param event Notused + * @param event PrimeFaces file upload event object */ - public void ajaxUploadImage(AjaxBehaviorEvent event) { - // String fileType = imageFile.getContentType(); - PictureController.importImage(recipe, imageFile); - } - + public void ajaxUploadImage(FileUploadEvent event) { + UploadedFile foo = event.getFile(); + + PictureController.importImage(recipe, foo.getContents()); + } /** * Remove images from recipe * diff --git a/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java b/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java index 4f63e0f..a14a572 100644 --- a/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java +++ b/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java @@ -1,8 +1,16 @@ package com.mousetech.gourmetj; +import javax.inject.Qualifier; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.ServletContextInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.web.filter.HiddenHttpMethodFilter; @SpringBootApplication @EntityScan(value = {"com.mousetech.gourmetj.persistence.model"}) @@ -11,4 +19,40 @@ public class SpringPrimeFacesApplication { public static void main(String[] args) { SpringApplication.run(SpringPrimeFacesApplication.class, args); } + + @Bean + public ServletContextInitializer initializer() { + return new ServletContextInitializer() { + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + servletContext.setInitParameter( + "primefaces.THEME", "bluesky"); + servletContext.setInitParameter( + "javax.faces.FACELETS_SKIP_COMMENTS", + "true"); + servletContext.setInitParameter( + "com.sun.faces.expressionFactory", + "com.sun.el.ExpressionFactoryImpl"); + servletContext.setInitParameter( + "primefaces.UPLOADER", "native"); + } + }; + } + +// @Bean +// public FilterRegistrationBean FileUploadFilter() { +// FilterRegistrationBean registration = new FilterRegistrationBean(); +// registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter()); +// registration.setName("PrimeFaces FileUpload Filter"); +// return registration; +// } + +// @Bean +// public FilterRegistrationBean hiddenHttpMethodFilterDisabled( +// HiddenHttpMethodFilter filter) { +// FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter); +// filterRegistrationBean.setEnabled(false); +// return filterRegistrationBean; +// } } diff --git a/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java b/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java index 1df8748..af5b29b 100644 --- a/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java +++ b/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java @@ -10,6 +10,7 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -177,19 +178,18 @@ public class PictureController { * Generate thumnail * * @param recipe Recipe to store into. - * @param imageFile Info about uploaded data. + * @param bs Info about uploaded data. * * CalledFrom @see * RecipeDetailBean#ajaxUploadImage(AjaxBehaviorEvent * event) */ public static void importImage(Recipe recipe, - Part imageFile) { - // imageFile.getContentType(); // ex: image/jpeg + byte[] bs) { try { byte[] bytes = null; - InputStream istream = imageFile.getInputStream(); + InputStream istream =new ByteArrayInputStream(bs); ImageInputStream stream = ImageIO.createImageInputStream(istream); BufferedImage bi = ImageIO.read(stream); diff --git a/src/main/resources/META-INF/resources/detailEdit.xhtml b/src/main/resources/META-INF/resources/detailEdit.xhtml index b5f55cc..42df5e1 100644 --- a/src/main/resources/META-INF/resources/detailEdit.xhtml +++ b/src/main/resources/META-INF/resources/detailEdit.xhtml @@ -24,11 +24,11 @@ text-align: left; } - + - + @@ -99,35 +99,32 @@ value="Description" /> -
+ - - - - - - - -
+