前台代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ include file="/commons/taglib.jsp" %><%@ include file="/commons/common.jsp" %>My JSP 'excelImp.jsp' starting page
后台文件接收:
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); Listlist = 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