使用JQuery和Java中的托管Bean处理Ajax上载

时间:2015-10-03 22:51:53

标签: java jquery ajax

这是范围: - Jquery DropzoneJS:用户放置文件但直到提交时才处理(autoProcessQueue: true) - 提交文件时应上传

到目前为止我所得到的: 这是我的Bean上传:

 public void fileUpload() throws  IOException {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest )context.getExternalContext().getRequest();
        HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
       if (!ServletFileUpload.isMultipartContent(request)) {
           throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
       }

       ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory());
       PrintWriter writer = response.getWriter();

       System.out.println(new File("C:/Users/ceo/Pictures/Saved Pictures/" + "images/"));
       try {
           List<FileItem> items = uploadHandler.parseRequest(request);

           for (FileItem item : items) {
               if (!item.isFormField()) {
                   SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
                   String name = fmt.format(new Date()) + item.getName();

                       File file = new File("C:/Users/ceo/Pictures/Saved Pictures/uploads/images/", name);
                       item.write(file);
                       imagepath = "/uploads/images/" + name;

                       System.out.println("uploaded");
                       this.setImagepath(imagepath);
               }
           }
       } catch (FileUploadException e) {
               throw new RuntimeException(e);
       } catch (Exception e) {
               throw new RuntimeException(e);
       } finally {

           writer.close();
       }


    }

来自:

$(document)
                .ready(
                        function() {
                            var myDropzone = new Dropzone("div#upload", {
                                url : "#{usermanagerMB.fileUpload()}",
                                autoProcessQueue: true,
                                method : "post",
                                maxFiles : 1,
                                uploadMultiple: false,
                                maxfilesexceeded : function(file) {
                                    this.removeAllFiles();
                                    this.addFile(file);
                                },
                                error: function (file, response) {
                                file.previewElement.classList.add("dz-error");
                                }
                            });

由于这是在第一页上,只需运行项目即可:

Caused by: java.lang.IllegalArgumentException: Request is not multipart, please 'multipart/form-data' enctype for your form.
    at empsuite.managedbean.UsermanagerManagedBean.fileUpload(UsermanagerManagedBean.java:231)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:165)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:84)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:157)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:238)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:154)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    ... 22 more

我需要知道的事情: - 为什么在我没有选择任何文件或做任何动作的情况下会发生这种情况? - 如何让bean上传文件?

1 个答案:

答案 0 :(得分:0)

使用omnifaces ajax处理程序,这很容易完成(ajax.oncomplete)