使用Struts2和Uploadify无法上传文件 [英] File uploading can not work using Struts2 and Uploadify

查看:80
本文介绍了使用Struts2和Uploadify无法上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSP页面如下:

$(function() {  
    $("#file_upload").uploadify({  
        'height': 27,
        'width': 80,
        'buttonText':'浏览',
        'swf':'<%=basePath%>admin/tupian/js/uploadify.swf',
        'uploader': '<%=basePath%>Imguploadoper.img',
        'auto' : false,
        'fileTypeExts' : '*.jpg'
        });
});

这是我的Java代码:

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
    //this line returns null
    List items = upload.parseRequest(request);
    Iterator itr = items.iterator();
    while (itr.hasNext()) {
        ......
    }
} catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
out.flush();
out.close();

upload.parseRequest(request)返回null.我真的不知道原因.

解决方案

在Struts2中上传时,这是一个常见错误.您不应该在操作中解析请求.我相信您已经在操作中编写了Java代码.因此,Struts2通过 MultipartRequestWrapper 使用ServletActionContext,请参见 我们如何上传文件.. >

然后 fileUpload拦截器 defaultStack的一部分,使用maltipart请求获取所有可接受的文件,可接受的文件名和可接受的内容类型,并将它们放在操作上下文中.

然后 params拦截器,它是defaultStack,使用该动作上下文参数,将它们置于动作属性中.

在打包多部分请求时(由Dispatcher完成),在实例化包装器时进行解析,如果上传成功且没有错误,则可以检查saveDir中的文件.

要执行文件上传,请确保您提交了多部分请求,即表单enctype属性为"multipart/form-data",并且拦截器应用于显式引用它们的操作,或使用拦截器的defaultStack隐式引用.在操作中,使用getter/setter创建用于文件名,内容类型和文件的属性.如果上传成功,请在操作属性中检查文件.

要了解更多信息,您可以练习以下示例:

My JSP page like this:

$(function() {  
    $("#file_upload").uploadify({  
        'height': 27,
        'width': 80,
        'buttonText':'浏览',
        'swf':'<%=basePath%>admin/tupian/js/uploadify.swf',
        'uploader': '<%=basePath%>Imguploadoper.img',
        'auto' : false,
        'fileTypeExts' : '*.jpg'
        });
});

Here is my java code:

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
    //this line returns null
    List items = upload.parseRequest(request);
    Iterator itr = items.iterator();
    while (itr.hasNext()) {
        ......
    }
} catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
out.flush();
out.close();

upload.parseRequest(request) returns null. I really don't know the reason.

解决方案

This is a common mistake when uploading in Struts2. You shouldn't parse the request in the action. I believe that you've written the java code in the action. So, Struts2 handles multipart request via the MultipartRequestWrapper, which is using the configuration constant

struts.multipart.parser=jakarta

that corresponds to the multipart request adapter JakartaMultiPartRequest, used to parse the request and put files to the location defined by this constant struts.multipart.saveDir, if this constant isn't set then javax.servlet.context.tempdir used by default.

You can get MultipartRequestWrapper using ServletActionContext, see How do we upload files.

Then fileUpload interceptor, which is a part of the defaultStack, using the maltipart request get all accepted files, accepted file names and accepted content types and put them to the action context.

Then params interceptor, which is a part of the defaultStack, using that action context params, put them to the action properties.

When multipart request wrapped, which is done by the Dispatcher, and parsed when wrapper is instantiated, you can check files in the saveDir, if uploading finished without errors.

To perform file uploading make sure you submit the multipart request, i.e the form enctype attribute is "multipart/form-data" and interceptors are applied to the action explicitly referencing them or implicitly using defaultStack of interceptors. In the action create properties with getters/setters for file names, content types, and files. If your uploads are succeeded then check your files in the action properties.

To learn more you can exercise these examples:

这篇关于使用Struts2和Uploadify无法上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆