AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-struts: How to upload files in struts 2?

i want to upload file in a folder resided in root folder. and file name in database using struts2. Please help me.

java x 210
struts x 31
Posted On : 2014-04-18 23:26:23.0
profile Garima Gupta - anyforum.in Garima Gupta
596129026212
up-rate
5
down-rate

Answers


To upload file in struts2 we need to use struts2 built-in FileUploadInterceptor. The File Upload Interceptor is based on MultiPartRequestWrapper, which is applied automatically to the current request if it contains the file element.
Before we implement the concept, we need to be sure that commons-io.jar file is present in the classpath. Following are the required Jar files to upload files using struts2.


required jar files to upload file in struts2

Now Let´s see the action class source code. Which adds the file upload functionality to our application. Create a java class as follows named FileUploadAction.java.

FileUploadAction.java:
--------------------------------

package in.anyforum;

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 File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;

public String execute() {
try {
String filePath = servletRequest.getSession().getServletContext().getRealPath(?/?).concat("Folder_Name resided in ROOT");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
/*** this.userImageFileName returns the file name, You can put database connection code to insert the file name into database below *****/
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());

return INPUT;
}
return SUCCESS;
}

public File getUserImage() {
return userImage;
}

public void setUserImage(File userImage) {
this.userImage = userImage;
}

public String getUserImageContentType() {
return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {
return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}

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

}
}


------------------------------------------------------------------------------------------------------------------------------

In above class file we have declared few attributes:

private File userImage: It contains actual uploaded File.
private String userImageContentType: This string contains the Content Type of uploaded file.
private String userImageFileName: This string contains the file name of uploaded file.

Note: we have implemented interface org.apache.struts2.interceptor.ServletRequestAware in above action class. This is to get servletRequest object to get the real path. We have used FileUtil.copyFile() method of commons-io package to copy the uploaded file on a location as you mentioned in String filePath variable.

Now let´s see the code the for input form and other JSP pages:

FileUpload.jsp:
---------------------

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Upload File</title>
</head>

<body>
<h2>Struts2 File Upload Example- anyforum.in</h2>
<s:actionerror />
<s:form action="userImage" method="post" enctype="multipart/form-data">
<s:file name="userImage" label="User Image" />
<s:submit value="Upload" align="center" />
</s:form>
</body>
</html>

Success.jsp:
------------------

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Success: Uploading File</title>
</head>
<body>
<h2>Struts2 File Upload Example</h2>

File Name: <s:property value="userImageFileName"/> Uploaded successfully.
<br/>
Uploaded Image:
<br/>
<img src="/Folder_Name resided in ROOT/<s:property value="userImageFileName"/>"/>


</body>
</html>


Struts.xml:
------------------

<action name="userImage"
class="in.anyforum.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">Success.jsp</result>
<result name="input">FileUpload.jsp</result>
</action>



Note: In above entry as we have specified two parameter to fileUpload interceptor, maximumSize and allowedTypes. These are optional parameters that we can specify to interceptor. The maximumSize param sets the maximum file size that can be uploaded. By default this is 2MB. And the allowedTypes param specify the allowed content types of file which can be uploaded.

As in above example we are uploading image so we mention some acceptable image types. You can modify it as per your need. and also it´s maximum size.

Posted On : 2014-04-19 00:25:52
Satisfied : 2 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
72933916624
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in