jsp如下
點擊(此處)摺疊或打開
-
<form enctype="multipart/form-data" name=testform method=post action=Testaction>
-
<table>
-
<tr>
-
<td><font size=2>批量上傳:</font><input type="file" name="test" size="10"><br></td>
-
<td><input type="submit" name="批量上傳" size="10"value="批量上傳"><br></td></tr></table><br>
- </form>
點擊(此處)摺疊或打開
-
package control;
-
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.PrintWriter;
-
import java.io.RandomAccessFile;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
public class Testaction extends HttpServlet {
-
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
-
}
-
-
/**
-
* The doPost method of the servlet. <br>
-
*
-
* This method is called when a form has its tag value method equals to post.
-
*
-
* @param request the request send by the client to the server
-
* @param response the response send by the server to the client
-
* @throws ServletException if an error occurred
-
* @throws IOException if an error occurred
-
*/
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
//接收上傳文件內容中臨時文件的文件名
-
String tempFileName = new String("tempFileName");
-
//tempfile 對象指向臨時文件
-
File tempFile = new File("D:/"+tempFileName);
-
//outputfile 文件輸出流指向這個臨時文件
-
FileOutputStream outputStream = new FileOutputStream(tempFile);
-
//得到客服端提交的所有數據
-
InputStream fileSourcel = request.getInputStream();
-
//將得到的客服端數據寫入臨時文件
-
byte b[] = new byte[1000];
-
int n ;
-
while ((n=fileSourcel.read(b))!=-1){
-
outputStream.write(b,0,n);
-
}
-
-
//關閉輸出流和輸入流
-
outputStream.close();
-
fileSourcel.close();
-
-
//randomFile對象指向臨時文件
-
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
-
//讀取臨時文件的第一行數據
-
randomFile.readLine();
-
//讀取臨時文件的第二行數據,這行數據中包含了文件的路徑和文件名
-
String filePath = randomFile.readLine();
-
System.out.println(filePath);
-
//得到文件名
-
int position = filePath.lastIndexOf('\\');
-
CodeToString codeToString = new CodeToString();
-
String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
-
//重新定位讀取文件指針到文件頭
-
randomFile.seek(0);
-
//得到第四行回車符的位置,這是上傳文件數據的開始位置
-
long forthEnterPosition = 0;
-
int forth = 1;
-
while((n=randomFile.readByte())!=-1&&(forth<=4)){
-
if(n=='\n'){
-
forthEnterPosition = randomFile.getFilePointer();
-
forth++;
-
}
-
}
-
-
//生成上傳文件的目錄
-
File fileupLoad = new File("F:/MyEclipse/Manager/WebRoot/file","upLoad");
-
fileupLoad.mkdir();
-
//saveFile 對象指向要保存的文件
-
File saveFile = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
-
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
-
//找到上傳文件數據的結束位置,即倒數第四行
-
randomFile.seek(randomFile.length());
-
long endPosition = randomFile.getFilePointer();
-
int j = 1;
-
while((endPosition>=0)&&(j<=4)){
-
endPosition--;
-
randomFile.seek(endPosition);
-
if(randomFile.readByte()=='\n'){
-
j++;
-
}
-
}
-
-
//從上傳文件數據的開始位置到結束位置,把數據寫入到要保存的文件中
-
randomFile.seek(forthEnterPosition);
-
long startPoint = randomFile.getFilePointer();
-
while(startPoint<endPosition){
-
randomAccessFile.write(randomFile.readByte());
-
startPoint = randomFile.getFilePointer();
-
}
-
randomAccessFile.close();
-
randomFile.close();
-
tempFile.delete();
-
-
TestExcel t=new TestExcel();
-
t.add();
-
-
-
}
-
-
-
-
- }
點擊(此處)摺疊或打開
-
package control;
-
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
-
import org.apache.commons.logging.Log;
-
import org.apache.commons.logging.LogFactory;
-
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 db.DB;
-
import db.Test_table;
-
public class TestExcel {
-
//記錄類的輸出信息?
-
static Log log = LogFactory.getLog(TestExcel.class);
-
//獲取Excel文檔的路徑?
-
public static String filePath = "F://MyEclipse//Manager//WebRoot//file//upLoad//test.xls";
-
public void add() {
-
try {
-
// 創建對Excel工作簿文件的引用?
-
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));
-
// 在Excel文檔中,第一張工作表的缺省索引是0
-
// 其語句爲:HSSFSheet sheet = workbook.getSheetAt(0);?
-
HSSFSheet sheet = wookbook.getSheet("Sheet1");
-
//獲取到Excel文件中的所有行數
-
int rows = sheet.getPhysicalNumberOfRows();
-
//遍歷行
-
for (int i = 0; i < rows; i++) {
-
// 讀取左上端單元格?
-
HSSFRow row = sheet.getRow(i);
-
// 行不爲空
-
if (row != null) {
-
//獲取到Excel文件中的所有的列?
-
int cells = row.getPhysicalNumberOfCells();
-
String value = "";
-
//遍歷列?
-
for (int j = 0; j < cells; j++) {
-
//獲取到列的值?
-
HSSFCell cell = row.getCell(j);
-
if (cell != null) {
-
switch (cell.getCellType()) {
-
case HSSFCell.CELL_TYPE_FORMULA:
-
break;
-
case HSSFCell.CELL_TYPE_NUMERIC:
-
value += cell.getNumericCellValue() + ",";
-
break;
-
case HSSFCell.CELL_TYPE_STRING:
-
value += cell.getStringCellValue() + ",";
-
break;
-
default:
-
value += "0";
-
break;
-
}
-
}
-
}
-
// 將數據插入到sqlserver數據庫中
-
String[] val = value.split(",");
-
DB db=new DB();
-
Test_table jBean=new Test_table();
-
String sql ="insert into test_table(num1,num2,num3) values('"+val[0]+"','"+val[1]+"','"+val[2]+"')";
-
-
int count=db.getInserttest(sql, jBean);
-
System.out.println("------------------"+sql);
-
if(count>0){
-
//關閉文件輸入、輸出
-
-
-
}
-
}}
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
- }
點擊(此處)摺疊或打開
-
public class DB {
-
-
private static DB db = null;
-
private static Connection conn = null;
-
private static PreparedStatement pstmt = null;
-
private static Statement stmt = null;
-
//單例模式,new DB類的時候,創建唯一對象,只初始化一次,
-
//注意:不要顯式閉此static中的Connection和Statement對象,否則拋空指針異常
-
static{
-
try {
-
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
-
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Market" ,"sa","a");
-
stmt = conn.createStatement();
-
System.out.println("--------初始化---------");
-
} catch (ClassNotFoundException e) {
-
System.out.println("---------- 加載數據庫驅動類時發生異常: ----------");
-
e.printStackTrace();
-
} catch (SQLException e) {
-
System.out.println("------------ getConnection()方法發生異常--------------");
-
}
-
}
-
public PreparedStatement prepareStmt(String sql){
-
PreparedStatement pstmt = null;
-
try {
-
pstmt = conn.prepareStatement(sql);
-
} catch (SQLException e) {
-
System.out.println("-------------prepareStmt()方法發生異常-------------------");
-
e.printStackTrace();
-
}
-
return pstmt;
-
}
-
//執行查詢所有記錄操作
-
public ResultSet exeQuery(String sql){
-
ResultSet rs = null;
-
try {
-
rs = stmt.executeQuery(sql);
-
} catch (SQLException e) {
-
System.out.println("------------exeQuery()方法發生異常: --------------------");
-
e.printStackTrace();
-
}
-
return rs;
-
}
-
public void exeUpdate(String sql){
-
try{
-
stmt.executeUpdate(sql);
-
} catch(SQLException e){
-
System.out.println("------------- exeUpdate()方法發生異常------------------");
-
e.printStackTrace();
-
}
-
}
-
//關閉PreparedStatement對象
-
public void closePstmt(PreparedStatement pstmt){
-
try{
-
pstmt.close();
-
pstmt = null;
-
}catch(SQLException e){
-
System.out.println("-------------------- DB.closePstmt()方法發生異常 -------------------------");
-
e.printStackTrace();
-
}
-
}
-
//關閉ResultSet對象
-
public void closeRs(ResultSet rs){
-
try{
-
if(rs!=null)
-
rs.close();
-
// rs = null;
-
}catch(SQLException e){
-
System.out.println("-------------------- DB.closePstmt()方法發生異常 -------------------------");
-
e.printStackTrace();
-
}
-
}
-
public Connection getDB()
-
{
-
return conn ;
- }
相關熱門文章
給主人留下些什麼吧!~~
評論熱議