Greysh's Blog Greysh

Friday May 01, 2009

WEB.XML


<?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">
 <welcome-file-list>
  <welcome-file>fileUpload.jsp</welcome-file>
 </welcome-file-list>
 <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>
</web-app>


struts2.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>
 <include file="struts-default.xml" />
 <include file="struts_image.xml" />
</struts>


upload page


<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>File Upload</title>
 </head>
 <body>
  <s:form action="/image/image!addImage.action" method="post"
   enctype="multipart/form-data">
   <s:file name="excelImage" theme="simple" />
   <s:submit name="submit" value="Upoad" theme="simple" />
  </s:form>
 </body>
</html>


action


package com.greysh.action;


import java.io.File;
import java.io.IOException;
import java.sql.SQLException;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;


public class FileAction extends ActionSupport {


 private static final long serialVersionUID = -382362366152017940L;
 
    private File excelImage;// Upload File


    private String excelImageContentType; //Content type


    private String excelImageFileName; // Upload file Name 
   public String addImage() throws SQLException, IOException {
   String targetDir = ServletActionContext.getServletContext()
     .getRealPath("/upload");
   File target = new File(targetDir, excelImageFileName);
   FileUtils.copyFile(excelImage, target);
   System.out.println(excelImageFileName);
   return "success";
 }


 public File getExcelImage() {
  return excelImage;
 }


 public void setExcelImage(File excelImage) {
  this.excelImage = excelImage;
 }


 public String getExcelImageContentType() {
  return excelImageContentType;
 }


 public void setExcelImageContentType(String excelImageContentType) {
  this.excelImageContentType = excelImageContentType;
 }


 public String getExcelImageFileName() {
  return excelImageFileName;
 }


 public void setExcelImageFileName(String excelImageFileName) {
  this.excelImageFileName = excelImageFileName;
 }



}


action config


<?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>
 <package name="image" extends="struts-default" namespace="/image">
  <action name="image"
   class="com.greysh.action.FileAction">
   <result name="success">/fileSuccess.jsp</result>
  </action>
 </package>
</struts>

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed