This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes File is not uploaded using struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File is not uploaded using struts2" Watch "File is not uploaded using struts2" New topic
Author

File is not uploaded using struts2

Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
Hi ,

I am working in struts2 with websphere6.1 on RAD7. When i try to upload the file using file upload interceptor. i failed .
I got this error.

[6/11/13 15:25:23:048 IST] 00000027 SystemOut O Server path:C:\WorkSpace_Struts\StrutsEx7\DemoFileUpload\WebContent
[6/11/13 15:25:23:095 IST] 00000027 FileUploadInt I org.apache.struts2.interceptor.FileUploadInterceptor intercept Removing file myFile \tmp\upload_6e068ff1_13f3220716b__7ff9_00000003.tmp
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R java.lang.NullPointerException
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at java.io.File.<init>(File.java:260)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.action.FileUploadAction.execute(FileUploadAction.java:60)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at java.lang.reflect.Method.invoke(Method.java:615)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:268)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:761)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:673)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:688)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.wswebcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:96)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:737)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:697)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.wswebcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:113)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3184)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:254)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1433)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:111)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:454)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:383)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:263)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:195)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:743)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:873)
[6/11/13 15:25:23:048 IST] 00000027 SystemErr R at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1469)


Please help. Thanks in advance.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

What code do you have here?

FileUploadAction.java line#60


Swastik
Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
Hi,

FileUploadAction.java File:

package com.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements ServletRequestAware {

/**
*
*/
private static final long serialVersionUID = 1L;

private File myFile;
private String myFileContentType;
private String myFileFileName;

private HttpServletRequest servletRequest =null;

public File getMyfile() {
return myFile;
}

public void setMyfile(File myfile) {
this.myFile = myfile;
System.out.println("file" );
}

public String getMyFileContentType() {
return myFileContentType;
}

public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
System.out.println("content" );
}

public String getMyFileFileName() {
return myFileFileName;
}

public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}

public String execute() {
try{

String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.myFileFileName);
FileUtils.copyFile(this.myFile, fileToCreate);

}catch(Exception e){
e.printStackTrace();
return INPUT;

}


return SUCCESS;

}


}


upload.jsp File

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Upload</title>
</head>
<body>
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="Upload Your File"></s:file>
<br/>
<s:submit label="UPLOAD"></s:submit>
</s:form>
</body>
</html>

struts.xml File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.multipart.maxSize" value="1000000" />



<package name="default" extends="struts-default">
<action name="upload" class="com.action.FileUploadAction">
<interceptor-ref name="basicStack"></interceptor-ref>
<interceptor-ref name="fileUpload">

</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>

struts.properties File
struts.multipart.saveDir=/tmp

In my application, filepath are not set on FileUploadAction as per my understanding.If this is a problem, then how it will set on FileUploadAction.
Please give some suggestion.
Thanks in advance.
Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
HI,

Line No. 60 in FileUploadAction:

File fileToCreate = new File(filePath, this.myFileFileName);
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Now try to find out what values do you have in the following two variables

filePath
and
this.myFileFileName
Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
HI,

[6/11/13 17:18:53:942 IST] 00000027 SystemOut O Server path:C:\WorkSpace_Struts\StrutsEx7\DemoFileUpload\WebContent
[6/11/13 17:18:53:942 IST] 00000027 SystemOut O FileName:null

I got this message in logs.
filePath = Server Path
this.myFileFileName = FileName

FileName is null means interceptor don't get file or filepath.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Bhawna,

Your set/get method naming conventions don't look right. I may be wrong, but for your reference you can refer to the following example.

http://www.mkyong.com/struts2/struts-2-file-upload-example/
Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
HI,

Thanks Swastik.It's working.
But i want to ask someting ie when i add

String filePath = servletRequest.getSession().getServletContext().getRealPath("/");

this line in execute method of action.

first web page opened but after that i got 404 error.When i removed this line,code worked without any error.
Could you please suggest me.Why this error come?
Thanks in advance.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

If you remove that line, how come this is working?

Bhawna Gupta
Greenhorn

Joined: May 28, 2013
Posts: 27
HI Swastik,

I am not talking about my application. I refered that example http://www.mkyong.com/struts2/struts-2-file-upload-example/ which you suggested.
I refered whole example .That worked very nice.But when i try to add this line only
String filePath = servletRequest.getSession().getServletContext().getRealPath("/"); in action class.I got 404 error.

But ,now that example working with the above line also.
I did small mistake in struts.xml ie i didn't add <interceptor-ref name="basicStack"></interceptor-ref> .
When i added this line , my new app was working and file uploaded in server also.

action in struts.xml file:




FileUploadAction2 class:



Thanks Swastik for your help.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

You are welcome Bhawna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File is not uploaded using struts2
 
Similar Threads
log4j setup
No Dialect mapping for JDBC type: -1
hibernate exception
org.apache.struts2.dispatcher.Dispatcher serviceAction Could not find action or result
Validator framework vs JBoss issue.