But i am trying to upload file from one location and try to save some else location but Interceptor comes into picture and its make
a some changes to Unknown to me.
I was changed this code like,
package com.kogent.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;
import bean.Files;
public class FileUploadAction extends ActionSupport implements ServletRequestAware
{
private HttpServletRequest servletRequest;
String systemFilePath = "D:\\suraj";
public String uploadFile()
{
System.out.println("uploadedFileName = " + this.uploadedFileName);
System.out.println("uploadFile = " + this.uploadedFile);
try
{
System.out.println("In try block. \n");
String realPath = servletRequest.getRealPath("/");
System.out.println(" Server path = " + realPath);
File filePath = new File(systemFilePath);
if(!filePath.exists())
{
filePath.mkdir();
System.out.println("Directory Made. \n");
}
File fileToCreate = new File(filePath,this.uploadedFileName);
FileUtils.copyFile(this.uploadedFile,fileToCreate);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
addActionError(e.getMessage());
}
return SUCCESS;
}
}
struts.xml
<
struts>
<package name="default" extends="tiles-default">
<action name="upload_file" method="uploadFile" class="com.kogent.action.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="error" type="tiles">error.def</result>
<result name="success" type="tiles">success.def</result>
</action>
</struts>
But still isn't working its thrown an Exception like, as well as
uploadedFileName and uploadFile it printed an null values.
Exception :-
Jul 15, 2010 12:44:08 PM org.apache.struts2.dispatcher.Dispatcher getSaveDir
INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
uploadedFileName = null
uploadFile = null
In try block.
Server path = D:\FileUploading_Tiles\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUploading_Tiles\
java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at com.kogent.action.FileUploadAction.uploadFile(FileUploadAction.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
......................
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
Jul 15, 2010 12:44:08 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
INFO: Removing file uploadfile D:\FileUploading_Tiles\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\FileUploading_Tiles\upload_239d1afe_129d4f4053b__8000_00000002.tmp
Jul 15, 2010 12:44:08 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
INFO: Removing file uploadfile D:\FileUploading_Tiles\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\FileUploading_Tiles\upload_239d1afe_129d4f4053b__8000_00000002.tmp
Thanks.