Monday, April 22, 2013

Upload .csv file in struts2

fileupload.jsp



<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<html>
<head>
 <s:head />
   <title>File Upload Page</title>
</head>
 <s:actionerror/>
 <body>
   <table border="0" align="center" bordercolor="red">
    <s:form action="uploadAction" method="POST" enctype="multipart/form-data">
 <tr>
      <td style="color: red;">
    Chose File:    <s:file name="uploadFile"  size="40" cssStyle="color:green"/>
        <s:iterator value="fieldErrors.uploadFile"><s:property/></s:iterator>
      </td>
 </tr>
  <tr>
       <td>
          <s:submit value="Upload" name="submit" cssStyle="color:green"/>
      </td>
 </tr>
    </s:form>
    </table>
 </body>
 </html>


FileUploadAction.java



package action;
import java.io.File;

import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import sqldao.DaoFactory;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;


private File uploadFile;
private String uploadFileContentType;
private String uploadFileFileName;

public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}

public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}

public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
DaoFactory dao = DaoFactory.getDao("sql");

public String execute()
{
try{
if(uploadFileFileName==null){
addFieldError("uploadFile", "file is required");
return INPUT;
}
String name = getUploadFileFileName();
        String []fileext = name.split("\\.");
        String ext = fileext[1];
if(ext.equals("csv")){
ServletContext context = ServletActionContext.getServletContext();
        String filePath = context.getRealPath("csvfile");
        String csv_location  = filePath+"/";
                 File fileToCreate = new File(csv_location, uploadFileFileName);
                 FileUtils.copyFile(uploadFile, fileToCreate);
                 String filePathname = csv_location+uploadFileFileName;
                 String filerealpath  = filePathname.replace("\\", "/");
        dao.insertdata(filerealpath);
}else{
  addFieldError("uploadFile", "Upload only csv file");
  return INPUT;
        }
}catch(Exception e)
{
e.printStackTrace();
            addActionError(e.getMessage());
            return INPUT;
}
return SUCCESS;
}

public Object getModel(){

return null;
}

}


struts.xml


<?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.multipart.maxSize" value="164703120" />
     <constant name="struts.ui.theme" value="simple"/>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
   
        <action name="uploadAction" class="action.FileUploadAction"> 
        
            <interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams"> dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods"> input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods"> input,back,cancel,browse</param>
</interceptor-ref>
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>


web.xml




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <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>







3 comments:

  1. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS TRAINING IN BTM LAYOUT | AWS TRAINING IN BANGALORE
    AWS Training in Marathahalli | AWS Training in Bangalore

    ReplyDelete
  2. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    online Python training
    python training in chennai

    ReplyDelete
  3. Thank you for sharing such a nice post!
    Best Training Institute in Bangalore BTM. My Class Training Bangalore training center for certified course, learning on Software Training Course by expert faculties, also provides job placement for fresher, experience job seekers.
    Software Training Institute in Bangalore

    ReplyDelete