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>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值