需求分析
本需求是基于JSP页面实现的.
上传word文档或者将word文档存到一个文件夹进行压缩再上传.
对于压缩的文件夹需要解压出来.
解析word文档里面的表格,并把表格内容存放到数据库.
把文件夹里面的数据清空.
需求实现
(直接看代码吧.有注释滴.如果有疑问可以提出来哦.)
//设置临时存放目录
String tempPath = "/temp/mobileimport";
File file = new File(tempPath);
if(!file.exists()){
file.mkdir();
}
DiskFileUploadEx fu = new DiskFileUploadEx();
//设置最大文件尺寸,
//得到所有文件
List fileItems = fu.parseRequestEx(request);
Iterator i = fileItems.iterator();
String tail = "";
//依次处理每一个文件
while(i.hasNext()){
FileItem fi = (FileItem)i.next();
if(fi.getName().equals("") && fi.getSize()==0){
System.out.println("1111");
continue;
}
String fileName = fi.getName();
fi.write(new File(tempPath+"/"+fileName));
tail = getFileTail(fileName);// 获取文件后缀
String fString = tempPath +"/"+fileName;
if(tail.equals("docx")){
parseWord(request,response, fString);
}else if(tail.equals("zip")){
//调用解压方法
System.out.println(fString);
unZipFiles(request,response,new File(fString), tempPath);
}
//解析word文档
//删除所有目录下所有文件
deleteDir(tempPath);
out.print("导入成功!");
//System.out.println(fString.substring(0,fString.indexOf('.')));
}
<%!
//解压文件
public static void unZipFiles(HttpServletRequest request,HttpServletResponse response,File zipFile,String descDir) throws IOException {
// request.setCharacterEncoding("utf-8");
File pathFile = new File(descDir);
List<String> pathList = new ArrayList<String>();
if (!pathFile.exists()){
pathFile.mkdirs();
}
/**
* ZipFile类用于从zip文件中读取条目
* getEntries()返回ZIP文件条目中的枚举
*/
ZipFile zip = new ZipFile(zipFile);
for (Enumeration entries = zip.getEntries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry) entries.nextElement();
entry.setUnixMode(644);//解决Linux乱码
String zipEntryName = entry.getName();
System.out.println(zipEntryName);
InputStream in = zip.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(in);
String outPath = (descDir+"/"+zipEntryName).replaceAll("\\*","/");
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0,outPath.lastIndexOf("/")));
if (!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()){
continue;
}
//输出文件路径信息
String suff = getFileTail(outPath);// 获取文件后缀
if(suff.equals("docx")){
pathList.add(outPath);
}
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0){
out.write(buf1,0,len);
}
bis.close();
in.close();
//out.flush();
out.close();
}
zip.close();
System.out.println("*******************解压完毕********************");
System.out.println("*******************解析文档********************");
for(String path:pathList){
parseWord(request,response, path);
}
}
%>
<%!//清空文件夹
public static boolean deleteDir(String path){
File file = new File(path);
if(!file.exists()){//判断是否待删除目录是否存在
System.err.println("文件夹不存在");
return false;
}
String[] content = file.list();//取得当前目录下所有文件和文件夹
for(String name : content){
File temp = new File(path, name);
if(temp.isDirectory()){//判断是否是目录
deleteDir(temp.getAbsolutePath());//递归调用,删除目录里的内容
temp.delete();//删除空目录
}else{
if(!temp.delete()){//直接删除文件
System.err.println(name+":删除失败!");
}
}
}
return true;
}
%>
<%!
这部分可能涉及公司的东西就不暴露了,主要是解析word文档内容和入库.
%>
public static boolean checkAddress(String address){
if (address.equals("广东省")){
return true;
}
System.out.println(address);
String regex="((?<province>[^省]+省|.+自治区)|上海|北京|天津|重庆)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|.+区|.+镇|.+局)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
Matcher m= Pattern.compile(regex).matcher(address);
String province=null,city=null,county=null,town=null,village=null;
List<Map<String,String>> table=new ArrayList<Map<String,String>>();
Map<String,String> row=null;
while(m.find()){
row=new LinkedHashMap<String,String>();
province=m.group("province");
row.put("province", province==null?"":province.trim());
city=m.group("city");
row.put("city", city==null?"":city.trim());
county=m.group("county");
row.put("county", county==null?"":county.trim());
town=m.group("town");
row.put("town", town==null?"":town.trim());
village=m.group("village");
row.put("village", village==null?"":village.trim());
table.add(row);
}
System.out.println(table);
if (table.size()==0){
System.out.println("问题属地格式存在问题!");
return false;
}else{
return true;
}
}
public static Date changeDate(String date) throws Exception{
//日期转换 4月13----转成Date
if(date==null || date.length()==0){
return null;
}
Calendar calendar = Calendar.getInstance();
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = date.substring(0,date.indexOf("月"));
String day = date.substring(date.indexOf("月")+1,date.length());
String stringDate = year+"-"+month+"-"+day;
//将String转换成Date
DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
Date resultDate = fmt.parse(stringDate);
return resultDate;
}
//获取后缀
public static String getFileTail(String fileName){
if (fileName.indexOf('.')>0){
return fileName.substring(fileName.indexOf('.')+1,fileName.length());
}else{
return "";
}
}