blueimp jquery文件上传空文件上传结果struts2,文件项目为空

时间:2013-04-20 19:36:11

标签: java jquery file-upload struts2 blueimp

我使用的是Blueimp,服务器端是Java,Struts2。我无法找到使用Java的示例,无论如何我设法使用示例代码,但是当我尝试上传单个文件时,我得到"Empty file upload result"。 HTML部分是相同的,我不会在这里粘贴,因为它可能会很长。

jQuery是:

$(document).ready(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

    if (window.location.hostname === 'blueimp.github.com') {
        // Demo settings:
        $('#fileupload').fileupload('option', {
            url: '//jquery-file-upload.appspot.com/',
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            process: [
                {
                    action: 'load',
                    fileTypes: /^image\/(gif|jpeg|png)$/,
                    maxFileSize: 20000000 // 20MB
                },
                {
                    action: 'resize',
                    maxWidth: 1440,
                    maxHeight: 900
                },
                {
                    action: 'save'
                }
            ]
        });
        // Upload server status check for browsers with CORS support:
        if ($.support.cors) {
            $.ajax({
                url: '//jquery-file-upload.appspot.com/',
                type: 'HEAD'
            }).fail(function () {
                $('<span class="alert alert-error"/>')
                    .text('Upload server currently unavailable - ' +
                            new Date())
                    .appendTo('#fileupload');
            });
        }
    } else {
        // Load existing files:
        $('#fileupload').each(function () {
            var that = this;
            $.getJSON(this.action, function (result) {
                if (result && result.length) {
                    $(that).fileupload('option', 'done')
                        .call(that, null, {result: result});
                }
            });
        });
    }

});

行动:

@Namespace("/")
@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("basicStack")
})
public class UploadAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{

    HttpServletRequest req;
    HttpServletResponse res;
  //  private File fileUploadPath=new File("c:\\temp\\");
    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();

    public List<File> getUploads() {
        return uploads;
    }

    public void setUploads(List<File> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileNames() {
        return uploadFileNames;
    }

    public void setUploadFileNames(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentTypes() {
        return uploadContentTypes;
    }

    public void setUploadContentTypes(List<String> uploadContentTypes) {
        this.uploadContentTypes = uploadContentTypes;
    }

    @Action(value="upload", results = { @Result(name="success", type="json")
    })
    public String uploadFiles() throws IOException
    {
        System.out.println("upload1");
        System.out.println("files:");
        for (File u: uploads) {
            System.out.println("*** "+u+"\t"+u.length());
        }
        System.out.println("filenames:");
        for (String n: uploadFileNames) {
            System.out.println("*** "+n);
        }
        System.out.println("content types:");
        for (String c: uploadContentTypes) {
            System.out.println("*** "+c);
        }
        System.out.println("\n\n");
        if (!ServletFileUpload.isMultipartContent(req)) {
            throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
        }
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.req=hsr;
    }

    @Override
    public void setServletResponse(HttpServletResponse hsr) {
        this.res=hsr;
    }

}

正如我所说,我已经更改了操作文件,但我仍然获得文件的所有空值,并且在Firebug的GET响应中,我看到"Request is not multipart, please 'multipart/form-data' enctype for your form"

谢谢&amp;问候。

1 个答案:

答案 0 :(得分:5)

您可以使用fileUpload interceptor来解析"multipart/form-data"次请求。它在Struts2调度程序的准备操作中使用由commons-fileupload包装的相同MultipartRequestWrapper实现。有关如何使用您可以找到here的示例进行文件上传的更多信息。