• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in file upload with struts 2?

 
Ritesh Dwa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


struts.xml


ProcessUploadAction.java


Output:
File name:null
java.lang.NullPointerException

can anyone suggest me, what is the problem?

Thank you in advance.

 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritesh ,

struts inteceptor remove the temp file once the file is uploaded so you need to copy files with file. Also as far as nullpointer exception is concern you need to check the configuration. I have used your code in sample application and uploaded file with minor changes.

You need to copy the uploaded file then you will be able to see it.

Jsp:


Action:


struts.xml:



It did work for me . You need to store the uploaded file in db or some location on server as struts inteceptor removes the file uploaded and you wont be able to see in your system. It gives you the info in the console like
INFO: Removing file upload E:\workspace1.5\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\struts2\upload__3c0968c2_139dd3c2d69__8000_00000001.tmp .

You can print the filename in setuploadfilename method where you can see the full path where the struts is uploading the file.

Regards
Jatan
 
Ritesh Dwa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jatan.

After changing the configuration, it worked.

Again thanks a lot.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic