上传文件示例:
index.jsp 页面:
index.jsp :
<form id="form1" name="form1" method="post" action="file.do?method=fileUpload" enctype="multipart/form-data">
<input name="file" type="file" size="20" >
</form>
后台action处理:
public ModelAndView fileUpload(HttpServletRequest request, HttpServletResponse response) { try { // //转型为MultipartHttpServletRequest MultipartHttpServletRequest mutilrequest = (MultipartHttpServletRequest) request; MultipartFile file = mutilrequest.getFile("file");//获得上传的文件 String path = request.getSession().getServletContext().getRealPath("WEB-INF/upload/");//得到上传服务器的路径 //获得文件名 String filename = file.getOriginalFilename(); //获得输入流 InputStream input = file.getInputStream(); //写入文件 byte[] b = new byte[1048576]; path+="\"+filename; File ff = new File(path); if(ff.exists()){ ff.delete(); } ff.createNewFile(); //文件流写到服务器端 FileOutputStream os = new FileOutputStream(path); // int len = input.read(b); // os.write(b,0,len); int len; while ((len = input.read()) != -1) { os.write(len); } input.close(); os.close(); //读取excel 联系人数据, List list = ReadExcel.readExcel(path,dxMap); for(int i=0;i<list.size();i++){ Map map = (HashMap)list.get(i); messAcceptService.insertMessAccepter_map(map); } if(ff.exists()){ ff.delete(); } } catch (Exception e) { e.printStackTrace(); } return list(request, response); }
使用POI读取文件excel的方法:
public class ReadExcel { /** * 读取excel文件内容 * @param fileName excel文件名称 * @param dmap excel中列名及对应的属性 ,keyCol: 列标题,姓名, key : 列标题对应的属性值,name * @return * @throws IOException */ public static List readExcel(String fileName,Map dmap) throws IOException { InputStream is = new FileInputStream(fileName); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); List list = new ArrayList(); Map map = null; //存放值 Map cmap = null; // 循环工作表sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet sheet = hssfWorkbook.getSheetAt(numSheet); if (sheet == null) { continue; } // 循环row HSSFRow row0 = sheet.getRow(0); if(row0==null){ break; } //初始化列值 cmap = new HashMap(); //循环获得列名 相对应的索引 for(int num=0;num<=row0.getLastCellNum();num++){ HSSFCell c0= row0.getCell((short) num); String val = getValue(c0); //标题值 if(!"".equals(val)){ Iterator it0 = dmap.entrySet().iterator(); //遍历列索引 while(it0.hasNext()){ Entry obj = (Entry) it0.next(); //key 列名 //姓名:name String colName = null==obj.getKey()?"":obj.getKey().toString(); String keyName = null==obj.getValue()?"":obj.getValue().toString(); if(num==0){ cmap.put(keyName, new Integer(-1)); //默认值 } if(val.indexOf(colName)>-1){ cmap.put(keyName, new Integer(num)); } } } } for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { HSSFRow row = sheet.getRow(rowNum); if (row == null) { continue; } map = new HashMap(); Iterator it = cmap.entrySet().iterator(); //遍历列索引 while(it.hasNext()){ Entry obj = (Entry) it.next(); Object val = obj.getValue(); //索引 Object key = obj.getKey(); //键 int col_index = Integer.parseInt(val.toString()); //列名所对应的索引 HSSFCell cell00 = row.getCell((short)col_index); map.put(key, getValue(cell00)); } list.add(map); } } return list; } }
注意,其中dMap的值是通spring注入到controller中的,
<bean id="excel_col" class="java.util.HashMap"> <constructor-arg> <map> <entry key="姓名" value="name"/> <entry key="性别" value="sex"/> </map> </constructor-arg> </bean> <bean id="FileController" class="com.FileController" parent="baseActionController" p:fileService-ref="fileService" p:methodNameResolver-ref="methodNameResolver" <property name="dMap" ref="excel_col"/> <property name="filename" value="name.xls"/> </bean>