码迷,mamicode.com
首页 > 编程语言 > 详细

JAVAWeb SSH框架 上传文件,如2007的EXCEL

时间:2018-03-11 23:57:14      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:extends   16px   cte   throw   ref   tomcat7   real   框架   on()   

 下面的代码是上传EXCEL的代码,其实,就是在上传文件到服务器,代码都差不多,只是接收的文件的类型改一下即可。
 本人用的服务器是tomcat7,所以上传的文件保存在D:\MyWork2014\.metadata\.me_tcat7\webapps\Graduate\uploadExcel
1.jsp 用的是struts2 标签
 
 importExcel.jsp
  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    
    <title>上传文件</title>
    
    <link rel="stylesheet" type="text/css" href="styles.css">
     
  </head>
  
  <body>
    <h4>导入Excel文件</h4>
     <s:form action="UploadExcel" name="ImportForm" enctype="multipart/form-data" method ="post">
         <div><s:file name="upload"></s:file>
         <span><input type="submit" value="确定上传" 
         style="background-color:#bbbbbb;color:white;width:90px;height:30px"/>
         </span>
         </div>
    </s:form>
  </body>
</html>

 

2 struts.xml

  

    </action>
               <action name="UploadExcel" class="uploadAction" method="uploadExcel">
                <result name="uploadExcelSuccess">international.jsp</result>    
                <result name="error">importExcel.jsp</result>    
                <param name="allowedTypes">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream</param>  
                 <!-- //允许上传的文件类型,这个是2007EXCEL,即XLSX后缀 -->
            </action>  

 

3.applicationContext-action.xml

  

    <bean id="uploadAction" class="com.international.action.uploadExcelAction" scope="prototype">
        
    </bean>

 

4. uploadExcelaction.java

package com.international.action;

import java.io.File;

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

import com.opensymphony.xwork2.ActionSupport;

public class uploadExcelAction extends ActionSupport{
    
    private File upload;               //用于接住jsp传过来的EXCEL文件
    private String uploadFileName;    //这个值不用进行处理,就能得到你传过来的EXCEL的文件名
    private String uploadContentType;  //uploadContentType这个值不用进行处理,就能得到你传过来的EXCEL文件的类型
    //如: 如果是2007的EXCEL,就是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    
     private String allowedTypes;        //接住在struts2设置的值,用于进行文件类型验证
     private String savePath; //设置绝对路径,用于存放上传的EXCEL
     
     public uploadExcelAction(){}

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getAllowedTypes() {
        return allowedTypes;
    }

    public void setAllowedTypes(String allowedTypes) {
        this.allowedTypes = allowedTypes;
    }

    public String getSavePath() {
           return savePath = ServletActionContext.getServletContext().getRealPath(
                    "/uploadExcel");
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    
    public String uploadExcel() throws Exception {
        // 验证文件格式
//        boolean flag = false;
//        System.out.println("uploadFileName: "+ uploadFileName + " uploadContentType: " + uploadContentType);
//        System.out.println("savePath:" + getSavePath() );
//        String[] allowedTypesStr = allowedTypes.split(",");
//        for (int i = 0; i < allowedTypesStr.length; i++) {
//            if (uploadContentType.equals(allowedTypesStr[i])) {
//                flag = true;
//            }
//        }
//        if (flag == false) {
//            System.out.println("上传文件格式错误");
//            return "error";
//        }
        //保存文件
        File newExcel = new File(getSavePath() + "\\" + uploadFileName);
        if (newExcel.exists()) {
            newExcel.delete();
        }
        try {
            FileUtils.copyFile(upload, newExcel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 删除临时文件
        upload.delete();
        return "uploadExcelSuccess";
    }
     
     
}

 

JAVAWeb SSH框架 上传文件,如2007的EXCEL

标签:extends   16px   cte   throw   ref   tomcat7   real   框架   on()   

原文地址:https://www.cnblogs.com/chendezhen/p/8546912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!