<form action="insertBatchBlack.action" enctype="multipart/form-data" method="post" id="sendform" name="sendform" οnsubmit="return submitForm();">
上传文件时 form表单按上面顺序填写,action要填写,否则找不到aciton 中 insertBatchBlack()方法
上传文件如下代码:
private File mobilefile;
private String mobilefileContentType;
private String mobilefileFileName;
public File getMobilefile() {
return mobilefile;
}
public void setMobilefile(File mobilefile) {
this.mobilefile = mobilefile;
}
public String getMobilefileContentType() {
return mobilefileContentType;
}
public void setMobilefileContentType(String mobilefileContentType) {
this.mobilefileContentType = mobilefileContentType;
}
public String getMobilefileFileName() {
return mobilefileFileName;
}
public void setMobilefileFileName(String mobilefileFileName) {
this.mobilefileFileName = mobilefileFileName;
}
/**批量添加
* @author SLF
* @return
*/
public String insertBatchBlack(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
if(adddemo!=null&&!"".equals(adddemo)){
try {
adddemo=URLDecoder.decode(adddemo,"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(adddemo+" "+uidstr);
//****************************************************************
if(!mobilefileContentType.equals("text/plain")){
request.setAttribute("ErrorMessage", "读取文件类型错误,请选择txt类型文件");
return "fail";
}
ArrayList<String> desmobilelist=this.readFileByLines(mobilefile);
if(desmobilelist==null){
request.setAttribute("ErrorMessage", "读取文件格式错误");
return "fail";
}
int count=desmobilelist.size();
if(count>10000){
count=10000;
}
int successcount=0;
int repeatcount=0;
int sucindex=0;
int exindex=0;
for (int i = 0; i < count; i++) {
String uidarr [] = uidstr.split(",");
for (int j = 0; j < uidarr.length; j++) {
int userid = Integer.parseInt(uidarr[j]);
//System.out.println(desmobilelist.get(i)+"======================="+userid);
int exitsCount = authorityDao.getTdBlackTotal(0, 0, userid, null, null, desmobilelist.get(i));
if (exitsCount>0){
exindex++;
continue;
}else{
int addnum = authorityDao.insertBlack(desmobilelist.get(i),userid,adddemo);
if(addnum>0){
sucindex++;
}
}
}
if(sucindex>0){
successcount++;
}
if(exindex>0){
repeatcount++;
}
}
String batchretv = "总数:"+count+",成功个数:"+successcount+","+"重复个数:"+repeatcount+"。";
request.setAttribute("batchretv", batchretv);
//*****************************************************************
return "success";
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public ArrayList<String> readFileByLines(File file) {
BufferedReader reader = null;
ArrayList<String> desmobilelist=new ArrayList<String>();
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
if(tempString.length()!=11){//文件内数据格式
desmobilelist=null;
break;
}
String str=tempString;
desmobilelist.add(str);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return desmobilelist;
}