日期:2014-05-16  浏览次数:20500 次

《JavaWeb---简单应用---练习JDBC,JSTL》---上传Excel文件,利用工具包解析,将数据储存到数据库中,可以查询删除

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>上传查看表格</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	body{
	text-align: center;
	}
	table,td{
	height: 30px;
	text-align: center;
	border: 1px green solid;
	border-collapse: collapse;
	}
	</style>
  </head>
  
  <body>
  <table>
  <tr style="color: red">
  <td>说明</td>
  <td>操作</td>
  </tr>
  <tr>
  <td> 查看数据库中的数据</td>
  <td>
    <a href="${pageContext.request.contextPath }/servlet/Read" >查看</a>
  </td>
  </tr>
  <tr>
  <td> 管理数据库中的数据</td>
  <td>
    <a href="${pageContext.request.contextPath }/servlet/Control" >管理</a>
  </td>
  </tr>
  <tr>
    <form action="${pageContext.request.contextPath }/servlet/Receive" enctype="multipart/form-data" method="post">
  <td>
    	<input type="file" name="filexls"/><br/>
  </td>
  <td>
    	<input type="submit" value="上传"/>
  </td>
    </form>
  </tr>
   </table>
  </body>
</html>
package com.strong.upload.receive;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

import com.strong.upload.dao.DaoUtils;
import com.strong.upload.javabean.User;

public class Receive extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置编码
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (isMultipart){
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);

			List<FileItem>  items = null;
			try {
				//转换请求数据
				items = upload.pa