struts2.0上传附件

Struts2.0上传附件。

 

Struts配置文件: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>

    <package name="templateManager" extends="struts-default">  

        <action name="uploadExcel" class="com.action.FileUploadAction" method="uploadAgentExcel">

   <result name ="success">/business/agent/chooseAgent.jsp</result>

        </action>    

   </package>

</struts>

 

Java类:

package com.action;

 

import java.io.File;

import java.io.IOException;

import java.util.Calendar;

 

import org.apache.commons.io.FileUtils;

import org.hdht.util.AppUtil;

import org.hdht.util.LoginUser;

 

/**

 * 附件上传的类(保存Excel数据源文件)

 * @author 胥大健

 * @time 201072

 *

 */

public class FileUploadAction {

       private File excel; // 附件。

       private String message;// 消息;

 

       public String getMessage() {

              return message;

       }

 

       public void setMessage(String message) {

              this.message = message;

       }

 

       /**

        * 保存导入Excel数据源文件的方法

        * @return

        * @throws IOException

        */

       public String uploadAgentExcel() throws IOException {

              String targetfile = AppUtil.getApplicationAbsolutePath()

                            + "/business/file";//excel数据源文件保存的位置。

String longname = “文件的名字”;

              File target = new File(targetfile, longname);//构造目标File对象

              File file1 = this.getExcel();//获得原始File对象

              FileUtils.copyFile(file1, target);//使用工具类将原始File拷贝到目标File中,并返回页面。

              return "success";

       }

 

      

       public File getExcel() {

              return excel;

       }

 

       public void setExcel(File excel) {

              this.excel = excel;

       }

 

}

 

Jsp页面:

<s:form action="uploadExcel" method="post" enctype="multipart/form-data" id="uploadForm" name="uploadForm">

<input type='file' name="excel" id='excel'/>

<input type="button" value="确定" id="dosubmit" onclick="ExcelImport(document.getElementById('excel').value)"/>

</s:form>

<iframe name="myIframe" style="display:none"></iframe>

 

 

 

 

你可能感兴趣的:(apache,jsp,xml,struts,Excel)