File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes problem in file upload with struts 2? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem in file upload with struts 2?" Watch "problem in file upload with struts 2?" New topic

problem in file upload with struts 2?

Ritesh Dwa

Joined: Sep 18, 2012
Posts: 7

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


File name:null

can anyone suggest me, what is the problem?

Thank you in advance.

jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297

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.




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.

Ritesh Dwa

Joined: Sep 18, 2012
Posts: 7
Thank you Jatan.

After changing the configuration, it worked.

Again thanks a lot.

I agree. Here's the link:
subject: problem in file upload with struts 2?
jQuery in Action, 3rd edition