File Upload Example:
When we are working in any web application development so we might to be get a chance to work on upload a file in the server.it seems very difficult code to upload file on server if we are using JSP/Servlet.
But when we will use the struts2 framework so this is very simple task to upload file on the server. It will just take 5-6 line of code in server side and your file will be uploaded .
Here i will go through step by step so you can get better understanding about uploading a file on the server.
First we need to understand that how file upload will work in struts2 framework.
Struts 2 use the service of FileUpload Interceptor . It adds the support for uploading files in the Server , FileUpload Interceptor is based on MultiPartRequestWrapper .
MultiPartRequestWrapper is automatically applied to the client request if the request contains the file element.
It adds the following parameters to the request. Suppose name of the file element is “aoiBlogFile”
1 [aoiBlogFile] : the uploading File (It is the File Type)
2 [aoiBlogFile]ContentType : the content type of the uploading file (It is the String Type)
3 [aoiBlogFile]FileName : the actual name of the file uploaded (It is the String Type)
So you have to define into the Action class like that :
Example :
Now you can see the overall example of uploading a file in struts2 at below.
JSP File :
Struts.xml File :
web.xml File :
FileUploading Action Class :
If you have any query so please free feel to ask .
Thanks for visiting my blog.
When we are working in any web application development so we might to be get a chance to work on upload a file in the server.it seems very difficult code to upload file on server if we are using JSP/Servlet.
But when we will use the struts2 framework so this is very simple task to upload file on the server. It will just take 5-6 line of code in server side and your file will be uploaded .
Here i will go through step by step so you can get better understanding about uploading a file on the server.
First we need to understand that how file upload will work in struts2 framework.
Struts 2 use the service of FileUpload Interceptor . It adds the support for uploading files in the Server , FileUpload Interceptor is based on MultiPartRequestWrapper .
MultiPartRequestWrapper is automatically applied to the client request if the request contains the file element.
It adds the following parameters to the request. Suppose name of the file element is “aoiBlogFile”
1 [aoiBlogFile] : the uploading File (It is the File Type)
2 [aoiBlogFile]ContentType : the content type of the uploading file (It is the String Type)
3 [aoiBlogFile]FileName : the actual name of the file uploaded (It is the String Type)
So you have to define into the Action class like that :
Example :
class uploadFile extends ActionSupport {
private File aoiBlogFile;
private String aoiBlogFileContentType;
private String aoiBlogFileFileName;
//its Getter/Setter.
}
JSP File :
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>File Upload Example - AOI Blog</
title
>
</
head
>
<
body
>
<
center
>
<
h4
>File Upload Example</
h4
>
</
center
>
<
s:form
action
=
"saveUploadedFile"
namespace
=
"/"
enctype
=
"multipart/form-data"
method
=
"post"
>
File Upload : <
s:file
name
=
"imageFile"
></
s:file
> <
br
/>
<
s:submit
value
=
"UPLOAD FILE"
></
s:submit
>
</
s:form
>
</
body
>
</
html
>
Struts.xml File :
<!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
=
"true"
/>
<
package
name
=
"struts2"
extends
=
"struts-default"
namespace
=
"/"
>
<
action
name
=
"showFileUploadPage"
class
=
"com.example.actions.FileUploadExample"
>
<
result
>/fileUpload.jsp</
result
>
</
action
>
<
action
name
=
"saveUploadedFile"
class
=
"com.example.actions.FileUploadExample"
method
=
"uploadFile"
>
<
result
name
=
"input"
>/fileUpload.jsp</
result
>
<
result
>/fileUpload.jsp</
result
>
</
action
>
</
package
>
</
struts
>
web.xml File :
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.4"
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
FileUploading Action Class :
package
com.example.actions;
import
java.io.File;
import
java.io.IOException;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.commons.io.FileUtils;
import
org.apache.struts2.interceptor.ServletRequestAware;
import
com.opensymphony.xwork2.ActionSupport;
public
class
FileUploadExample
extends
ActionSupport
implements
ServletRequestAware{
private
File imageFile;
private
String imageFileContentType;
private
String imageFileFileName;
private
HttpServletRequest httpServletRequest;
public
String execute() {
return
SUCCESS;
}
public
String uploadFile(){
System.out.println(
"imageFile "
+imageFile+
" imageFileContentType "
+imageFileContentType+
" imageFileFileName "
+imageFileFileName);
String mySavefile = httpServletRequest.getRealPath(
"/"
)+
"images\\myimage.jpg"
;
System.out.println(
"MySave File "
+mySavefile);
File theFile =
new
File(mySavefile);
try
{
FileUtils.copyFile(imageFile, theFile);
}
catch
(IOException e) {
e.printStackTrace();
}
return
SUCCESS;
}
public
File getImageFile() {
return
imageFile;
}
public
void
setImageFile(File imageFile) {
this
.imageFile = imageFile;
}
public
String getImageFileContentType() {
return
imageFileContentType;
}
public
void
setImageFileContentType(String imageFileContentType) {
this
.imageFileContentType = imageFileContentType;
}
public
String getImageFileFileName() {
return
imageFileFileName;
}
public
void
setImageFileFileName(String imageFileFileName) {
this
.imageFileFileName = imageFileFileName;
}
@Override
public
void
setServletRequest(HttpServletRequest request) {
this
.httpServletRequest = request;
}
}
If you have any query so please free feel to ask .
Thanks for visiting my blog.
No comments:
Post a Comment