<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type=text/javascript src="./js/upload.js"></script>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form name="form" action="Path" method="post" enctype="multipart/form-data">
文件:<javascript><input type="file" name="excel"/></javascript>
<input type="text" name="123">
<input type="submit" value="提交"/>
</form>
</body>
</html>
JSP页面
package com....;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 com...;
/*
- @describe 通过路径获取Excel文件,并将Excel文件数据传入DAO
- @author zhutianpeng
-
*/
public class ReadExcel {Map<Integer,String> map = new HashMap<Integer,String>();
List<Map<Integer,String>> list = new ArrayList<Map<Integer,String>>();
HSSFWorkbook workbook;
// ConnectorA a = new ConnectorA();public void readExcelFile(String filePath){
try {
FileInputStream excelFile = new FileInputStream(filePath);
workbook = new HSSFWorkbook(excelFile);
//读入Excel文件的第一个表
HSSFSheet sheet = workbook.getSheetAt(0);
//从文件第二行开始读取,第一行为标识行
for(int i=1;i<=sheet.getLastRowNum();i++){
System.out.println("行数:"+sheet.getPhysicalNumberOfRows());
HSSFRow row = sheet.getRow(i);
if(row==null){
continue;
}
for(int j=1;j<=row.getPhysicalNumberOfCells();j++){
if(row.getCell(j)!=null){
// 注意:一定要设成这个,否则可能会出现乱码
// row.getCell(j).setEncoding(HSSFCell.ENCODING_UTF_16);
String str = getCellValue(row.getCell(j));
map.put(j,str);
}
}
UploadExcelImpl ue = new UploadExcelImpl();
ue.uploadExcel(map);
map.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("【Excel路径有误,请重新确认Excel路径...】");
} catch (IOException e) {
e.printStackTrace();
System.out.println("【文件输入有误,请重新确定您要加入的文件...】");
}
}//传入cell的值,进行cell值类型的判断,并返回String类型
private static String getCellValue(HSSFCell cell){
String value = null;
//简单的查检列类型
System.out.println("cell.getCellType():"+cell.getCellType());
switch(cell.getCellType()) {case HSSFCell.CELL_TYPE_STRING://字符串 System.out.println("HSSFCell.CELL_TYPE_STRING:"+HSSFCell.CELL_TYPE_STRING); value = cell.getRichStringCellValue().toString(); break; case HSSFCell.CELL_TYPE_NUMERIC://数字 System.out.println("HSSFCell.CELL_TYPE_NUMERIC:"+HSSFCell.CELL_TYPE_NUMERIC); long dd = (long)cell.getNumericCellValue(); value = dd+""; break; case HSSFCell.CELL_TYPE_BLANK: System.out.println("HSSFCell.CELL_TYPE_BLANK:"+HSSFCell.CELL_TYPE_BLANK); value = ""; break; case HSSFCell.CELL_TYPE_FORMULA: System.out.println("HSSFCell.CELL_TYPE_FORMULA:"+HSSFCell.CELL_TYPE_FORMULA); value = String.valueOf(cell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BOOLEAN://boolean型值 System.out.println("HSSFCell.CELL_TYPE_BOOLEAN:"+HSSFCell.CELL_TYPE_BOOLEAN); value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: System.out.println("HSSFCell.CELL_TYPE_ERROR:"+HSSFCell.CELL_TYPE_ERROR); value = String.valueOf(cell.getErrorCellValue()); break; default: System.out.println("default"); break; } return value; }
}
Java
package com....;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.;
public class Path extends HttpServlet {
public Path() {
super();
}
public void destroy() {
super.destroy();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// String path = request.getServletPath();
// String p = request.getSession().getServletContext().getRealPath("/");
//创建文件项工厂
//ExcelUploadConnector euc = new ExcelUploadConnector();
ReadExcel r= new ReadExcel();
DiskFileItemFactory factory = new DiskFileItemFactory();
//创建解析数据请求项
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//开始解析
List<FileItem> list = upload.parseRequest(request);
for(int i=0;i<list.size();i++){ //从列表中遍历每一个文件
FileItem item = list.get(i);
if(item.isFormField()){
//输出表单字段值
System.out.println(new String(item.getString().getBytes("ISO-8859-1"),"utf-8"));
}else{
//处理文件
String fileName = item.getName();
System.out.println("file:"+fileName);
//获取文件扩展名
String extName = fileName.substring(fileName.lastIndexOf("."));
System.out.println("extName:"+extName);
//生成UUID文件名
String newName = UUID.randomUUID().toString();
String rootPath = getServletContext().getRealPath("\upload");
System.out.println(rootPath);
String newPath = rootPath+"/"+newName+extName;
System.out.println(newPath);
item.write(new File(newPath));
//euc.ConnectorDatabase(newPath);
r.readExcelFile(newPath);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}
Servlet
注意:
1、JSP页面Form表单中的 enctype="multipart/form-data" 是必须使用的,将表单中的内容以流的形式进行转发。
2、上传过程是必须将用户本地机的内容上传至服务器后才能进行相应的操作。
转自https://blog.51cto.com/zhutianpeng/1397422
转载于:https://blog.51cto.com/13995051/2310307