0

I am getting file name in action as null, uploaded from plupload plugin. how can i get the original file name. Please tell where is my mistake.

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

@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

i can see this on console according to print statements given.

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

is it the problem in my struts config? I am using annotations hence dont have struts.xml file. Or the problem is in plupload sending the file name, because when i retrived the name parameter i am getting some different name "o_17oq47949abc11n51pg11rnah06a.jpg" which is not the actual name. If so how can i get the original name?

Thanks and regards

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Aadam
  • 1,521
  • 9
  • 30
  • 60

3 Answers3

1

I still don't understand why you are using all this plugins to perform an operation that simple. Do you need AJAX upload ? If not, you can simply use <s:file /> to upload one or more files to Action, with automatic filesize check, filename and contenttype detection, by simply declaring three variables with the same prefix into your Action:

Action code:

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

JSP code:

<s:file name="fileUpload" />

Then you can perform a client-side (HTML5, with javascript) filesize check, a server-side filesize check, and set a server-side overall multipart request size (in Struts.xml, that you should have even when using Annotations).

You can read the details here: struts2 s:form element trims the s:url parameter in the action attribute

As a final suggestion: make something simple that works, then start customizing / extending it.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I know i can to it simply with s:form and s:file and also i know i can do multiple files. What i need is ajax upload and single field upload i mean if i want to upload multiple files in struts way then i need to have multiple s:file, i am looking for single file field to browse and multiple files to upload. Hope you understood what i need. Thankyou – Aadam Apr 22 '13 at 12:45
  • Moreover i think the file name it self is getting changed by plupload. Still i am not sure. – Aadam Apr 22 '13 at 12:48
  • Have you tried with Struts2-jQuery plugin ? http://stackoverflow.com/questions/5270288/upload-file-in-struts2-using-ajax You always need an s:file, but you simply upload it using jQuery to perfomr AJAX calls. It would be different for uploading with drag and drop, *then* you would need to write javascript stuff: http://html5doctor.com/drag-and-drop-to-server/ – Andrea Ligios Apr 22 '13 at 12:53
  • Actually everything seems to work, and also i like this plugin so wanted to use this only, the only problem is i am not getting the original file name. – Aadam Apr 22 '13 at 15:28
  • Finally in firebug i can see the file name changed in the post. So i think now its confirmed that plugin itself is changing the file name. – Aadam Apr 22 '13 at 15:32
  • Then I suggest you answer your own question adding some details and mark it as the accepted answer – Andrea Ligios Apr 22 '13 at 20:34
0

It is solved now. I set unique_names : false in plupload options. The content type and filename are null in struts action. I retrieved file name using getParameter("name");.

bgw
  • 2,036
  • 1
  • 19
  • 28
Aadam
  • 1,521
  • 9
  • 30
  • 60
0

Solution:

Add fileupload interceoptor in struts.xml (< interceptor-ref name="fileUpload" />)

Add the following properties in the action class.

    private String[] fileFileName;

    private String[] fileContentType;

    private File[] file;
Chethan C
  • 54
  • 7