


前面兩張是excel表格直接讀取時,前端能夠正常提交servlet進行資料驗證讀取寫入資料庫,后面是一張是在excel表格讀取驗證發現錯誤后,打開excel表格修改錯誤,重新上傳讀取資料,前端發起request請求,后端servlet無回應。求教各位是哪里出問題了么?如果我重新重繪頁面,相當于各個功能模塊關閉重繪回首頁再來上傳讀取檔案,就又可以了,不知道問題出在哪里。下面是部分前端后端的代碼。請各位幫忙指正下,謝謝
相關js部分
//批量匯入學生資訊
$("#import").click(function(){
$.messager.confirm("訊息提醒", "將批量匯入學生資訊!確認繼續", function(r){
if(r){
$("#batchImportDialog").dialog("open");
}
});
});
//批量添加學生資訊對話框
$("#batchImportDialog").dialog({
title: "批量匯入學生資訊",
width: 650,
height: 460,
iconCls: "icon-edit",
modal: true,
collapsible: false,
minimizable: false,
maximizable: false,
draggable: true,
closed: true,
buttons: [
{
text:'上傳檔案',
plain: true,
iconCls:'icon-user_add',
handler:function(){
/*var validate = $("#addForm").form("validate");
if(!validate){
$.messager.alert("訊息提醒","請檢查你輸入的資料!","warning");
return;
} else{
var gradeid = $("#add_gradeList").combobox("getValue");
var clazzid = $("#add_clazzList").combobox("getValue");*/
var form = new FormData(document.getElementById("batchImportForm"));
$.ajax({
type: "post",
url: "StudentServlet?method=BatchImportStudent&t="+new Date().getTime(),
data: form,
processData:false,
contentType:false,
success: function(msg){
if(msg=="success"){
$.messager.alert("訊息提醒","批量匯入學生資訊成功","info");
//重繪表格
$("#dataList").datagrid("reload");
$("#dataList").datagrid("uncheckAll");
}else{
$.messager.alert("訊息提醒",msg,"warning");
return;
}
}
});
}
},
]
});
$('#fb').filebox({
buttonText: '選擇檔案',
buttonAlign: 'right',
accept:'application/vnd.ms-excel',
});
html部分
<!-- 批量添加學生視窗 -->
<div id="batchImportDialog" style="padding: 10px">
<form id="batchImportForm" method="post" enctype="multipart/form-data">
<input id="fb" class="easyui-filebox" name="file" style="width:300px;"><br/>
<div id="p" class="easyui-progressbar" style="width:300px;"></div><br/>
<a id="batchImportBtn" hrf="#" class="easyui-linkbutton">上傳檔案</a>
</form>
</div>
后端代碼部分
//讀取excel表格資訊,批量匯入資料庫
private void batchImportStudent(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=utf-8");
System.out.println("批量上傳按鈕");
try {
//匯入資料庫表頭模板
String[] databaseTitleTemp= {"number","name","sex","phone","qq","clazzid","gradeid"};
String[] titleNameList= new String[databaseTitleTemp.length];
Object[] gradeclazzid=null;
//創建工廠
DiskFileItemFactory factory=new DiskFileItemFactory();
String path=this.getServletContext().getRealPath("/upload");
File f=new File(path);
if(!f.exists()) {
f.mkdirs();
}
//設定檔案的快取路徑
factory.setRepository(f);
//創建fileupload組件
ServletFileUpload fileupload=new ServletFileUpload(factory);
fileupload.setHeaderEncoding("gbk");
//決議request
List<FileItem>fileitems=fileupload.parseRequest(request);
PrintWriter writer=response.getWriter();
//遍歷集合
first:for(FileItem fileitem:fileitems) {
//判斷是否為普通欄位
if(fileitem.isFormField()) {
//獲得欄位名和欄位值
String name=fileitem.getFieldName();
String value=https://bbs.csdn.net/topics/fileitem.getString("gbk");
}else {
//上傳的檔案路徑
String filename=fileitem.getName();
//截取出檔案名
filename=filename.substring(filename.lastIndexOf("\\")+1);
//檔案名需要唯一
filename=UUID.randomUUID().toString()+"_"+filename;
//在服務器創建同名檔案
String webPath="/upload/";
String filepath=getServletContext().getRealPath(webPath+filename);
//創建檔案
File file=new File(filepath);
file.getParentFile().mkdirs();
file.createNewFile();
//獲得上傳檔案流
InputStream in=fileitem.getInputStream();
HSSFSheet sheet=bimportStudent.getSheet(in);
//獲取表格總行數
int rows=bimportStudent.getTotalRows(sheet);
//獲取表格總列數
int columns=bimportStudent.getTotalColumns(sheet);
//上傳表格首行模板
String[] titleNameTemp= {"學號","姓名","性別","電話","QQ","班級","年級"};
ExcelDataValidate excelDataValidate=new ExcelDataValidate();
//對比上傳表格和模板首行是否一致
String validateTitleInfo=excelDataValidate.titleNameCompare(titleNameTemp, bimportStudent, sheet);
if(!validateTitleInfo.equals("true")) {
writer.print(validateTitleInfo);
validateTitleInfo=null;
in.close();
break first;
}else {
for(int i=0;i<databaseTitleTemp.length;i++) {
titleNameList[i]=databaseTitleTemp[i];
}
}
//核查上傳表格資料有效性
int[] columnNum= {0,1}; //指定需要核查的列
int[] cellType= {HSSFCell.CELL_TYPE_NUMERIC,HSSFCell.CELL_TYPE_STRING}; //設定需要核查列的默認資料型別
String validateDataType=excelDataValidate.dataTypeValidate(rows, cellType, columnNum, bimportStudent, sheet);
if(!validateDataType.equals("true")) {
writer.print(validateDataType);
validateDataType=null;
in.close();
break first;
}
/*for(int r=1;r<rows;r++) {
boolean a=bimportStudent.checkCell(sheet, cellType[0], r, c[0]);
if(!a) {
System.out.println((r+1)+"行"+(c[0]+1)+"列"+"單元格資料型別與模板不一致");
writer.print((r+1)+"行"+(c[0]+1)+"列"+"單元格資料型別與模板不一致");
in.close();
break first;
}else {
continue;
}
}
for(int r=1;r<rows;r++) {
boolean a=bimportStudent.checkCell(sheet, cellType[1], r, c[1]);
if(!a) {
System.out.println((r+1)+"行"+(c[1]+1)+"列"+"單元格資料型別與模板不一致");
writer.print((r+1)+"行"+(c[1]+1)+"列"+"單元格資料型別與模板不一致");
in.close();
break first;
}else {
continue;
}
}*/
/*
* 對班級年級資訊進行核驗,資料如不存在,提醒先添加班級年級資訊
*/
for(int r=1;r<rows;r++) {
String clazzCell=bimportStudent.getCellValue(sheet, r, 5).getStringCellValue();
String gradeCell=bimportStudent.getCellValue(sheet, r, 6).getStringCellValue();
gradeclazzid=service.getGradeClazzId(gradeCell,clazzCell);
int size=gradeclazzid.length;
if(size==0) {
writer.print((r+1)+"行班級年級資訊有誤!如有需要請先新增該班級年級資訊");
in.close();
break first;
}
}
/*
* 批量匯入一個表格中只能匯入同一個班級的學生,如果存在不同班級跳出錯誤提醒
*/
for(int r=2;r<rows;r++) {
String clazzCell1=bimportStudent.getCellValue(sheet, r-1, 5).getStringCellValue();
String clazzCell2=bimportStudent.getCellValue(sheet, r, 5).getStringCellValue();
String gradeCell1=bimportStudent.getCellValue(sheet, r-1, 6).getStringCellValue();
String gradeCell2=bimportStudent.getCellValue(sheet, r, 6).getStringCellValue();
if(!clazzCell1.equals(clazzCell2)) {
writer.print((r+1)+"行班級不一致!請一次匯入一個班級");
in.close();
break first;
}
if(!gradeCell1.equals(gradeCell2)) {
writer.print((r+1)+"行年級不一致!請一次匯入一個班級");
in.close();
break first;
}
}
/*
* 驗證都通過之后執行下面批量匯入資料庫,呼叫ExcelTool類中指定首行名方法
*/
List<Student> list=bimportStudent.importExcelAssignTopRow(Student.class, sheet, titleNameList,gradeclazzid);
for(Student student:list) {
service.addStudent(student);
}
//回傳給easyui form的success訊息
writer.print("success");
//關流
in.close();
}
}
}catch(Exception e) {
throw new RuntimeException(e);
}
}
uj5u.com熱心網友回復:
剛又搗鼓了下,發現瀏覽器提示如下,上傳檔案發生改變,所以就不能上傳了。這個有辦法解決么?不要整個專案重繪
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69362.html
標籤:Web 開發
上一篇:事務傳播機制,搞懂。
