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>
</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>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>

View File

@ -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
*

View File

@ -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;
// }
}

View File

@ -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);

View File

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