Jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Servlet
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, IO Exception {//string path = Request.getservletpath ();//String P = request.getsession (). Getservletcontext (). GE
Trealpath ("/"); Create a file item factory//exceluploadconnector EUC = new ExceluploadcoNnector ();
Readexcel r= new Readexcel ();
Diskfileitemfactory factory = new Diskfileitemfactory ();
Create a resolution data request item Servletfileupload upload = new Servletfileupload (factory);
try {//start parsing list<fileitem> List = upload.parserequest (request);
for (int i=0;i<list.size (); i++) {//traverse each file from the list Fileitem item = List.get (i); if (Item.isformfield ()) {//Output form field value System.out.println (item.getstring (). g
Etbytes ("Iso-8859-1"), "Utf-8"));
}else{//Processing file String FileName = Item.getname ();
System.out.println ("File:" +filename);
Gets the file name extension String extname = filename.substring (Filename.lastindexof ("."));
System.out.println ("Extname:" +extname); Generate UUID filename 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);
A catch (Fileuploadexception e) {e.printstacktrace ());
catch (Exception e) {e.printstacktrace (); } public void Init () throws servletexception {}}
Java
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 get the Excel file through the path and pass the Excel file data into the 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);
Read the first table in the Excel file Hssfsheet sheet = workbook.getsheetat (0); Read from the second line of the file, the first behavior identifies the row for (int i=1;i<=sheet.getlastrownum (); i++) {System.out.println ("Number of rows:" +sh
Eet.getphysicalnumberofrows ());
Hssfrow row = Sheet.getrow (i);
if (row==null) {continue;
for (int j=1;j<=row.getphysicalnumberofcells (); j + +) {if (Row.getcell (j)!=null) {
Note: Be sure to set this, otherwise there may be garbled//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 path is incorrect, please reconfirm Excel path ... "");
catch (IOException e) {e.printstacktrace ();
System.out.println ("" The file entered incorrectly, please redefine the file you want to add ... "); }
}
//
The value of the incoming cell, which is judged by the cell value type, and returns the string type private static string Getcellvalue (Hssfcell cell) {string value = null;
Simple search column type System.out.println ("Cell.getcelltype ():" +cell.getcelltype ());
Switch (Cell.getcelltype ()) { Case hssfcell.cell_type_string://String System.out.println ("H
Ssfcell.cell_type_string: "+hssfcell.cell_type_string); Value = Cell.getrichstringcellvalue (). toString ();
Break Case hssfcell.cell_type_numeric://Digital System.out.println ("Hssfcell.cell_type_numeric:" +hssfcell.cell_ty
Pe_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_f
Ormula);
Value = string.valueof (Cell.getcellformula ());
Break Case Hssfcell.cell_type_boolean://boolean Type value 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; }
}
Attention:
1, JSP page form in the enctype= "Multipart/form-data" is necessary to use, the form of the content in the form of a stream forward. 2, upload process is the user's local machine must be uploaded to the server before the corresponding operation.