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

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

问题描述

我的 JSP 页面是这样的:

$(function() {$("#file_upload").uploadify({'高度':27,宽度":80,'buttonText':'浏览','swf':'<%=basePath%>admin/tupian/js/uploadify.swf','上传者': '<%=basePath%>Imguploadoper.img','自动':假,'fileTypeExts' : '*.jpg'});});

这是我的java代码:

ServletFileUpload upload = new ServletFileUpload(factory);upload.setHeaderEncoding("UTF-8");尝试 {//这一行返回空列表项 = upload.parseRequest(request);迭代器 itr = items.iterator();而 (itr.hasNext()) {......}} catch (FileUploadException e) {//TODO 自动生成的 catch 块e.printStackTrace();}out.flush();关闭();

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

解决方案

这是在 Struts2 中上传时常见的错误.您不应在操作中解析请求.我相信您已经在操作中编写了 java 代码.因此,Struts2 通过 MultipartRequestWrapper,即使用配置常量

struts.multipart.parser=jakarta

对应于多部分请求适配器 JakartaMultiPartRequest,用于解析请求并将文件放到这个常量定义的位置struts.multipart.saveDir,如果这个常量没有设置,那么javax.servlet.context.tempdir 默认使用.

你可以得到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:

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

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