Got fileupload working

This commit is contained in:
Tim Holloway 2022-01-01 14:09:25 -05:00
parent c051751e10
commit e15ad6dc62
5 changed files with 102 additions and 32 deletions

27
pom.xml
View File

@ -82,6 +82,33 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.xerial</groupId> <groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>

View File

@ -17,6 +17,8 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.servlet.http.Part; import javax.servlet.http.Part;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -888,13 +890,13 @@ public class RecipeDetailBean implements Serializable {
/** /**
* Load/replace images. Computes thumbnail. * Load/replace images. Computes thumbnail.
* *
* @param event Notused * @param event PrimeFaces file upload event object
*/ */
public void ajaxUploadImage(AjaxBehaviorEvent event) { public void ajaxUploadImage(FileUploadEvent event) {
// String fileType = imageFile.getContentType(); UploadedFile foo = event.getFile();
PictureController.importImage(recipe, imageFile);
} PictureController.importImage(recipe, foo.getContents());
}
/** /**
* Remove images from recipe * Remove images from recipe
* *

View File

@ -1,8 +1,16 @@
package com.mousetech.gourmetj; 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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; 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 @SpringBootApplication
@EntityScan(value = {"com.mousetech.gourmetj.persistence.model"}) @EntityScan(value = {"com.mousetech.gourmetj.persistence.model"})
@ -11,4 +19,40 @@ public class SpringPrimeFacesApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringPrimeFacesApplication.class, 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;
// }
} }

View File

@ -10,6 +10,7 @@ import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -177,19 +178,18 @@ public class PictureController {
* Generate thumnail * Generate thumnail
* *
* @param recipe Recipe to store into. * @param recipe Recipe to store into.
* @param imageFile Info about uploaded data. * @param bs Info about uploaded data.
* *
* CalledFrom @see * CalledFrom @see
* RecipeDetailBean#ajaxUploadImage(AjaxBehaviorEvent * RecipeDetailBean#ajaxUploadImage(AjaxBehaviorEvent
* event) * event)
*/ */
public static void importImage(Recipe recipe, public static void importImage(Recipe recipe,
Part imageFile) { byte[] bs) {
// imageFile.getContentType(); // ex: image/jpeg
try { try {
byte[] bytes = null; byte[] bytes = null;
InputStream istream = imageFile.getInputStream(); InputStream istream =new ByteArrayInputStream(bs);
ImageInputStream stream = ImageInputStream stream =
ImageIO.createImageInputStream(istream); ImageIO.createImageInputStream(istream);
BufferedImage bi = ImageIO.read(stream); BufferedImage bi = ImageIO.read(stream);

View File

@ -24,11 +24,11 @@
text-align: left; text-align: left;
} }
</style> </style>
<h:messages /> <h:messages id="messages"/>
<p:panel id="editorPanel" <p:panel id="editorPanel"
header="#{recipeDetailBean.recipe.title}" header="#{recipeDetailBean.recipe.title}"
> >
<h:form id="form1"> <h:form id="form1" enctype="multipart/form-data">
<p:tabView id="tabGroupClient" orientation="left" <p:tabView id="tabGroupClient" orientation="left"
activeIndex="#{userSession.detailTab}" activeIndex="#{userSession.detailTab}"
> >
@ -99,35 +99,32 @@
value="Description" value="Description"
/> />
<p:inputTextarea id="description" <p:inputTextarea id="description"
rows="10" escape="false" rows="10" cols="30" escape="false"
value="#{recipeDetailBean.recipe.description}" value="#{recipeDetailBean.recipe.description}"
/> />
<div id="picture"> <p:panel id="picPanel">
<img id="bigPix" <img id="bigPix"
src="/img/picture/?dt=#{recipeDetailBean.currentTime}" src="/img/picture/?dt=#{recipeDetailBean.currentTime}"
/> />
<!-- #{recipeDetailBean.recipe.id} --> </p:panel>
<p:fileUpload id="ctlUpload" <p:fileUpload id="ctlUpload"
mode="simple" label="Upload Image"
label="Upload image" fileUploadListener="#{recipeDetailBean.ajaxUploadImage}"
value="#{recipeDetailBean.imageFile}" global="true"
> mode="advanced" multiple="false"
<!-- <f:ajax update=":messages picPanel"
listener="recipeDetailBean.ajaxUploadImage" auto="true" sizeLimit="1000000"
execute="ctlUpload" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
render="picture" />
/> --> <p:button id="ctlDelImg"
</p:fileUpload> value="Delete Image"
<p:button id="ctlDelImg" >
value="Delete Image" <!-- <f:ajax
>
<!-- <f:ajax
listener="recipeDetailBean.ajaxDeleteImage" listener="recipeDetailBean.ajaxDeleteImage"
execute="ctlDelImg" execute="ctlDelImg"
immediate="true" render="picture" immediate="true" render="picture"
/> --> /> -->
</p:button> </p:button>
</div>
</p:panelGrid> </p:panelGrid>
</p:tab> </p:tab>
<p:tab id="ingredientsTab" <p:tab id="ingredientsTab"