4

我正在使用 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".

4

1 回答 1

5

您可以使用fileUpload拦截器来解析您的"multipart/form-data"请求。它在 Struts2 调度程序的准备操作中使用由MultipartRequestWrapper包装的相同commons-fileupload实现。更多关于如何通过示例文件上传的信息,您可以在此处找到。

于 2013-04-20T20:34:19.960 回答