jfinal form表单提交文件

前台代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/commons/taglib.jsp" %>
<%@ include file="/commons/common.jsp" %>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'excelImp.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="${basePath}/js/sg/jquery-1.9.1.js"></script>
  </head>
  <body>
    <form id="file_form" action="${basePath }/excel/upload" enctype="multipart/form-data"
        method="post"  onsubmit="return validate()">
        <input type="file" name="file" id="file_input" /> 
        <input type="submit" value="文件上传" id='upFile-btn'>
    </form>
  </body>
  <script type="text/javascript">
     
     function validate(){
         var fileName = $("#file_input").val();
         if (fileName === "") {
             alert("请选择文件");
             return false;
         }
         var fileType = (fileName.substring(fileName
                 .lastIndexOf(".") + 1, fileName.length))
                 .toLowerCase();
         if (fileType !== "xls" && fileType !== "xlsx") {
             alert("文件格式不正确,请选择excel文件!");
             return false;
         }

         $("#file_form").ajaxSubmit({
             dataType : "json",
             success : function(data, textStatus) {
                 if (data.result === "OK") {
                     alert("上传文件成功");
                 } else {
                     alert("文件格式错误");
                 }
                 return false;
             }
         });
         return true;
     }
    </script>

</html>

后台文件接收:

public void upload(){  
        try {
//            HttpServletRequest request = getRequest();  
//            String basePath = request.getContextPath();   
            String ftype = getPara("ftype");
            System.out.println("数据类型:"+ftype);
            //存储路径  
            String path = getSession().getServletContext().getRealPath(Preference._PATH);  
            UploadFile file = getFile("file");  
            String fileName = "";  
            if(file.getFile().length() > 200*1024*1024) {  
                System.out.println("文件长度超过限制,必须小于200M");  
                setAttr("result", "文件长度超过限制,必须小于200M");  
            }else{
                //上传文件
//                String type = file.getFileName().substring(file.getFileName().lastIndexOf(".")); // 获取文件的后缀  
//                fileName = System.currentTimeMillis() + type; // 对文件重命名取得的文件名+后缀  
//                String dest = path + "/" + fileName;  //新的文件路径+文件名
//                System.out.println("新的文件路径+文件名:"+dest);  
//                file.getFile().renameTo(new File(dest));  
//                //读取文件内容
//                File filen = new File(dest);
                List<String[]> list = POIUtil.readExcel(file.getFile());
                for (int i = 0; i < list.size(); i++) {
                    String[] str = list.get(i);
                    
                    Db.use("db1").update("insert into ics_tasks (query, taskid, tname, ttype, btype) "
                            + "values ('"+str[0]+"','"+JavaUtil.getCRC32(str[0])+System.currentTimeMillis()+"','"+str[1]+"','"+str[2]+"','"+str[3]+"')");
                    
                }
                
//                String realFile = basePath + "/" + Preference._PATH +  fileName;          
//                String fname="/"+fileName;
//                setAttr("fname", fname);  
//                setAttr("url", realFile);  
                
                setAttr("result", "OK");  
            }
        } catch (Exception e) {
            e.printStackTrace();
            setAttr("result", e.getMessage());  
        }  
              
        renderJson();  
    }  

参考:http://blog.csdn.net/the_first_c/article/details/72868119

原文地址:https://www.cnblogs.com/learningJAVA/p/7495819.html