基于Struts2的上传与下载

前台表单

<form action="" method="post" enctype="multipart/form-data"
       style="padding: 0px; height: 31px; line-height: 31px; display: inline;"
       name="inputForm" id="inputForm">
    <table border="0" cellpadding="0" cellspacing="0" align="center"
     width="100%">
     <tr>
      <td style="text-align: left;">
       <label>
       <input type="hidden" name="type" id="type" value="${type}"/>
       &nbsp;选中导入的Excel:
       <input type="file" name="excelFile" id="uf"
        style="width: 200px; height: 23px; line-height: 23px;" />
       <input type="button" value="导入" οnclick="input()"
        style="width: 50px; height: 23px;" />
       
       <input type="button" value="下载导入格式"
       οnclick="javascript:window.location.href='imp!download.ssl?type=${type}'"
        style="width: 100px; height: 23px;" /> 
       </label>
      </td>
      <td style="width: 80px; text-align: right;"></td>
     </tr>
    </table>
    </form>

前台验证js

  <script type="text/javascript" language="javascript">
//导入
function input() {
 var fileName = $("#uf").val();
 if (fileName == "") {
  alert("请选择要导入的Excel文件!");
 } else {
  var extension = fileName.substring(fileName.lastIndexOf(".") + 1,
    fileName.length);
  if (extension != null && extension != "") {
   var c_extension = extension.toLowerCase();
   if (c_extension == "xls") {
    document.inputForm.action = "imp!importDriver.ssl";
    $("#inputForm").submit();
   } else {
    alert("选择的文件格式不正确,请重新选择!");
   }
  } else {
   alert("选择时出现异常!");
  }

 }
}
</script>

 

action方法

File excelFile;// 导入文件

String excelFileFileName;// 导入文件的名称

/**
  * 批量导入数据
  *
  * @return
  */
 public String importDriver() {
  HttpServletRequest request = ServletActionContext.getRequest();
  Map session = ActionContext.getContext().getSession();// 获得session
  String realPath = ServletActionContext.getServletContext().getRealPath(
    "/ExcelUpload");
  String upfileFileName = "";
  String extendName = "";
  try {
   if (excelFile != null) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm");
    // 扩展名
    extendName = excelFileFileName.substring(excelFileFileName
      .lastIndexOf("."));
    upfileFileName = "导入机手" + df.format(new Date()) + extendName;
    File saveFile = new File(new File(realPath), upfileFileName);
    if (!saveFile.getParentFile().exists())
     saveFile.getParentFile().mkdirs();
    FileUtils.copyFile(excelFile, saveFile);
   }
  } catch (Exception e) {
   request
     .setAttribute("warnInfo",
       "<script>alert(\'上传失败\')</script>");
   e.printStackTrace();
  }
  String filePath = realPath + "/" + upfileFileName;
  if (!"".equals(extendName) && ".xls".equals(extendName)) {
   importService.importDriver(filePath);
  }
  request.setAttribute("warnInfo", "<script>alert(\'上传成功\')</script>");
  return "import";
 }

 public String download() {
  HttpServletRequest request = ServletActionContext.getRequest();
  String type = request.getParameter("type");
  setImportType(type);
  return "exe";
 }

 public InputStream getDownloadFile() {
  InputStream is = null;
  String path = "";
  path = ServletActionContext.getServletContext().getRealPath(
    "/ExcelUpload/导入机手格式.zip");
  try {
   is = new FileInputStream(path);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return is;
 }

 public String getFileName() {
  String name = "";
  try {// 解决下载文件中文文件名问题

   name = new String("导入机手格式.zip".getBytes("GBK"), "ISO8859-1");

  } catch (Exception e) {
   e.printStackTrace();
  }
  return name;
 }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值