Results 1 to 4 of 4
  1. #1
    ritesh_163 is offline Member
    Join Date
    Sep 2012
    Location
    Kathmandu
    Posts
    2
    Rep Power
    0

    Post Struts2, problem with file upload?

    hello,
    I am using struts 2.2.1 framework with jdk 1.6 and running in tomcat 7. In my web application, I want to upload excel file and read the content of that file. I have searched many websites and even apache website as well but I could not solve my problem.

    After uploading file, when I try to read the uploaded file, error occurs : java.lang.NullPointerException. I think the variable "upload" in java file, should automatically initialized after file upload. isn't it?

    I think my code is good, but its not working.

    upload page: upload.jsp
    Java Code:
        ......  
         <s:form action="processUpload" method="post" namespace="/" enctype="multipart/form-data">  
                    <s:file name="upload" label="Upload File"></s:file>  
                    <s:submit label="Submit" />  
                </s:form>  
        .....

    struts.xml
    Java Code:
        ......  
                <action name="processUpload" class="action.ProcessUploadAction">  
                    <interceptor-ref name="basicStack" />  
                    <interceptor-ref name="fileUpload">  
                        <param name="allowedTypes">application/ms-excel,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>  
                    </interceptor-ref>  
                    <result name="input">upload.jsp</result>  
                    <result name="success">upload.jsp</result>  
                    <result name="error">upload.jsp</result>  
                </action>  
        ......

    ProcessUploadAction.java
    Java Code:
        ....  
        private File upload;  
            private String uploadContentType;   
            private String uploadFileName;   
          
            public void setUpload(File upload) {  
                this.upload = upload;  
            }  
          
            public void setUploadContentType(String uploadContentType) {  
                this.uploadContentType = uploadContentType;  
            }  
          
            public void setUploadFileName(String uploadFileName) {  
                this.uploadFileName = uploadFileName;  
            }  
          
            public String execute() {  
                try{  
                    System.out.println("File name:"+upload);  
                }catch(Exception ex){  
                    ex.printStackTrace();  
               }  
                return SUCCESS;  
            } 
    ....

    Output:
    File name:null
    java.lang.NullPointerException

    can anyone suggest me, what is the problem?

    Thank you in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Struts2, problem with file upload?

    What does the file upload interceptor do?
    What happens if you remove it from the stack?

    Indeed, I'd pare this right back to simply trying to upload the file, then start adding bits in to see what breaks.
    Please do not ask for code as refusal often offends.

  3. #3
    ritesh_163 is offline Member
    Join Date
    Sep 2012
    Location
    Kathmandu
    Posts
    2
    Rep Power
    0

    Default Re: Struts2, problem with file upload?

    it worked, after changing configuration at struts.xml. I used default stack in interceptor.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Struts2, problem with file upload?

    OK.
    SO the fileUpload interceptor was the problem?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Facing Problem to upload Struts2 Based Web App
    By shahinengg in forum Struts
    Replies: 11
    Last Post: 01-13-2012, 09:37 AM
  2. problem with file upload
    By i4ani in forum Java Servlet
    Replies: 10
    Last Post: 10-21-2010, 12:44 PM
  3. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 12:21 AM
  4. Struts2 :File upload and div on same form
    By saqib in forum Web Frameworks
    Replies: 2
    Last Post: 08-19-2008, 11:41 AM
  5. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •