[英]JSF file upload on GAE
I'm trying to have a file upload element in my JSF over Google App Engine. 我正在尝试通过Google App Engine在我的JSF中添加文件上传元素。 I have browsed the web for several alternatives but none seem to work with GAE.
我已经在网上浏览了几种替代方法,但似乎没有一种可以与GAE一起使用。
I was able to do so using JSP and servlet with BlobstoreService but couldn't find a way to make it working with JSF. 我能够通过BlobstoreService使用JSP和servlet来做到这一点,但是找不到使其与JSF一起使用的方法。
As a workaround I was trying to see if there is a way to include a JSP within a JSF but I guess this isn't doable as well. 作为一种解决方法,我试图查看是否可以在JSF中包含JSP,但我想这也不可行。
Would be thankful to get a working example. 希望得到一个可行的例子。
Thanks! 谢谢!
First get library http://code.google.com/p/gmultipart/ and add to your project. 首先获取库http://code.google.com/p/gmultipart/并将其添加到您的项目中。 And than override class org.primefaces.webapp.filter.FileUploadFilter (just put in your src).
并且比重写org.primefaces.webapp.filter.FileUploadFilter类(只在您的src中放)。 There is code of class org.primefaces.webapp.filter.FileUploadFilter:
有类org.primefaces.webapp.filter.FileUploadFilter的代码:
package org.primefaces.webapp.filter;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.gmr.web.multipart.GFileItemFactory;
import org.primefaces.webapp.MultipartRequest;
public class FileUploadFilter implements Filter {
private final static Logger logger = Logger.getLogger(FileUploadFilter.class.getName());
private final static String THRESHOLD_SIZE_PARAM = "thresholdSize";
private final static String UPLOAD_DIRECTORY_PARAM = "uploadDirectory";
private String thresholdSize;
private String uploadDir;
public void init(FilterConfig filterConfig) throws ServletException {
thresholdSize = filterConfig.getInitParameter(THRESHOLD_SIZE_PARAM);
uploadDir = filterConfig.getInitParameter(UPLOAD_DIRECTORY_PARAM);
if(logger.isLoggable(Level.FINE))
logger.fine("FileUploadFilter initiated successfully");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
boolean isMultipart = ServletFileUpload.isMultipartContent(httpServletRequest);
if(isMultipart) {
if(logger.isLoggable(Level.FINE))
logger.fine("Parsing file upload request");
//start change
FileItemFactory diskFileItemFactory = new GFileItemFactory();
/* if(thresholdSize != null) {
diskFileItemFactory.setSizeThreshold(Integer.valueOf(thresholdSize));
}
if(uploadDir != null) {
diskFileItemFactory.setRepository(new File(uploadDir));
}*/
//end change
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
MultipartRequest multipartRequest = new MultipartRequest(httpServletRequest, servletFileUpload);
if(logger.isLoggable(Level.FINE))
logger.fine("File upload request parsed succesfully, continuing with filter chain with a wrapped multipart request");
filterChain.doFilter(multipartRequest, response);
} else {
filterChain.doFilter(request, response);
}
}
public void destroy() {
if(logger.isLoggable(Level.FINE))
logger.fine("Destroying FileUploadFilter");
}
}
In managed bean write method like: 在托管bean的写方法中,例如:
public void handleFileUpload(FileUploadEvent event) {
UploadedFile uploadedFile = event.getFile();
try {
String blobKey = BlobUtils.uploadImageToBlobStore(uploadedFile.getContentType(), uploadedFile.getFileName(), uploadedFile.getContents());
this.iconKey = blobKey;
} catch (IOException e) {
log.log(Level.SEVERE, "Ошибка при попытке загрузить файл в blob-хранилище", e);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка при попытке загрузить файл", event.getFile().getFileName() + " не загружен!");
FacesContext.getCurrentInstance().addMessage(null, msg);
return;
}
FacesMessage msg = new FacesMessage("Успешно.", event.getFile().getFileName() + " загружен.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
And that all. 而这一切。
First of all , I think that whatever you are doing with JSP should eventually work with JSF as well.. 首先,我认为您使用JSP所做的任何事情最终都应该也可以与JSF一起使用。
BUT, 但,
If you are looking for a file upload component for JSF , that works on GAE , 如果您正在寻找适用于GAE的JSF文件上传组件,
take a look at the PrimeFaces FileUpload 看看PrimeFaces FileUpload
Here is another link that got an explanation on what to do in order it to work on GAE : Primefaces File Upload Filter 这是另一个链接,该链接解释了如何在GAE上运行该文件 : Primefaces文件上传过滤器
(haven't tried it myself...) (我自己还没有尝试过...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.