GAE上的JSF文件上传

时间:2012-03-15 19:17:26

标签: google-app-engine jsf file-upload

我正在尝试通过Google App Engine在我的JSF中拥有一个文件上传元素。 我浏览了网页上的几个替代方案,但似乎都没有与GAE一起使用。

我能够使用JSP和servlet与BlobstoreService这样做但无法找到使其与JSF一起使用的方法。

作为一种解决方法,我试图看看是否有办法在JSF中包含JSP,但我想这也是不可行的。

感谢能得到一个有效的例子。

谢谢!

2 个答案:

答案 0 :(得分:4)

首先获取库http://code.google.com/p/gmultipart/并添加到您的项目中。 并覆盖类org.primefaces.webapp.filter.FileUploadFilter(只需放入你的src)。 有类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");
        }

    }

在托管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);

}

而这一切。

答案 1 :(得分:1)

首先,我认为无论你使用JSP做什么,最终都应该使用JSF ..

,但

如果您正在寻找JSF的文件上传组件,那么它适用于GAE,

查看PrimeFaces FileUpload

这是另一个链接,它解释了如何处理GAE:Primefaces File Upload Filter

(自己没试过......)