struts2文件名和内容类型为null,使用plupload上传

时间:2013-04-22 10:59:43

标签: struts2 plupload

我正在获取文件名作为null,从plupload插件上传。我怎样才能获得原始文件名。请告诉我的错误在哪里。

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
<head>
<title>test</title>
<sj:head compressed="false"/>
<link rel="stylesheet" type="text/css" href="plup/jquery.plupload.queue/css/jquery.plupload.queue.css" type="text/css" media="screen" />
<script type="text/javascript" src="plup/plupload.full.min.js"></script>
<script type="text/javascript" src="plup/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript">
/* Convert divs to queue widgets when the DOM is ready */
$(function(){
    function plupload(){
        $("#uploader").pluploadQueue({
            // General settings
            runtimes : 'html5,gears,browserplus,silverlight,flash,html4',
            url : 'uploads',
            max_file_size : '10mb',
            unique_names : true,
            chunk_size: '2mb',
            // Specify what files to browse for
            filters : [
                {title: "Image files", extensions: "jpg,gif,png"},
                {title: "Zip files", extensions: "zip"}
            ],
            resize: {width: 320, height: 240, quality: 90},

            // Flash settings
            flash_swf_url : 'plup/Moxie.swf',
            // Silverlight settings
            silverlight_xap_url : 'plup/Moxie.xap',
            multipart_params: {'user': 'admin', 'time': '2012-06-12'}
        });
    }
    plupload();
    $('#clear').click(function(){
        plupload();
    });
});
</script>

</head>

<body>
    <div>
        <div style="width: 750px; margin: 0px auto">
            <form id="formId" action="submit.action" method="post">
                <div id="uploader">
                    <p>Flash, Silverlight, Gears, BrowserPlus,HTML5 .</p>
                </div>
                <input type="button" value="Clear" id="clear"/>
            </form>
        </div>
    </div>
</body>

</html>

行动

@Action(value="plupUploaduploads")
        public String upload() throws Exception {
                isMultipart = ServletFileUpload.isMultipartContent(getReq());
                System.out.println(getReq().getParameter("value"));
                ServletContext servletContext = getReq().getServletContext();
                String filePath = servletContext.getRealPath("/");
                System.out.println(filePath);
                System.out.println(this.file);
                System.out.println(this.fileName);
                System.out.println(this.contentType);
                File theFile = new File("c:\\",this.getFileName());  
                FileUtils.copyFile(file,theFile); 
        return SUCCESS;
    }

//getters and setters

我可以根据给出的印刷声明在控制台上看到这一点。

o_17oq47949abc11n51pg11rnah06a.jpg
E:\Documents and Settings\Pluto\My Documents\NetBeansProjects\ShareApp\build\web\
c:\temp\upload__408094b5_13e30976641__7fea_00000003.tmp
null
null

是我struts配置中的问题吗?我正在使用注释因此没有struts.xml文件。或者问题是在plupload中发送文件名,因为当我重新获取name参数时,我得到一些不同的名称“o_17oq47949abc11n51pg11rnah06a.jpg”,这不是实际名称。如果是这样,我怎么能得到原来的名字?

谢谢和问候

3 个答案:

答案 0 :(得分:1)

我仍然不明白为什么你使用所有这些插件来执行一个简单的操作。你需要AJAX upload吗?如果没有,您只需使用<s:file /&gt;通过自动filesize检查,filenamecontenttype检测,将一个或多个文件上传到操作,只需在操作中声明三个具有相同前缀的变量:

行动代码:

private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
/* getters and setters */

JSP代码:

<s:file name="fileUpload" />

然后你可以执行客户端(HTML5,带有javascript)文件大小检查,服务器端文件大小检查,并设置服务器端整体多部分请求大小(在Struts.xml中,即使在使用时也应该如此)注释)。

您可以在此处阅读详细信息:struts2 s:form element trims the s:url parameter in the action attribute

作为最后的建议:制作一些有效的东西,然后开始定制/扩展它。

答案 1 :(得分:0)

现在解决了。我在unique_names : false选项中设置plupload。 struts操作中的内容类型和文件名为null。我使用getParameter("name");检索了文件名。

答案 2 :(得分:0)

解决方案:

在struts.xml中添加fileupload interceoptor(&lt; interceptor-ref name =&#34; fileUpload&#34; /&gt;)

在操作类中添加以下属性。

    private String[] fileFileName;

    private String[] fileContentType;

    private File[] file;