From b6c5e5d0d3df51796fe02204512352bbc2a10eae Mon Sep 17 00:00:00 2001 From: bigtage <896932646@qq.com> Date: Tue, 3 Nov 2015 10:30:21 +0800 Subject: [PATCH 1/3] ini2 --- src/com/spssol/action/package-info.java | 8 -------- src/com/spssol/bean/package-info.java | 8 -------- src/com/spssol/dao/package-info.java | 8 -------- src/com/spssol/module/package-info.java | 8 -------- src/com/spssol/service/package-info.java | 8 -------- src/com/spssol/util/package-info.java | 8 -------- 6 files changed, 48 deletions(-) delete mode 100644 src/com/spssol/action/package-info.java delete mode 100644 src/com/spssol/bean/package-info.java delete mode 100644 src/com/spssol/dao/package-info.java delete mode 100644 src/com/spssol/module/package-info.java delete mode 100644 src/com/spssol/service/package-info.java delete mode 100644 src/com/spssol/util/package-info.java diff --git a/src/com/spssol/action/package-info.java b/src/com/spssol/action/package-info.java deleted file mode 100644 index 370c5aa..0000000 --- a/src/com/spssol/action/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.action; \ No newline at end of file diff --git a/src/com/spssol/bean/package-info.java b/src/com/spssol/bean/package-info.java deleted file mode 100644 index 66cc243..0000000 --- a/src/com/spssol/bean/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.bean; \ No newline at end of file diff --git a/src/com/spssol/dao/package-info.java b/src/com/spssol/dao/package-info.java deleted file mode 100644 index 5fa1e9b..0000000 --- a/src/com/spssol/dao/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.dao; \ No newline at end of file diff --git a/src/com/spssol/module/package-info.java b/src/com/spssol/module/package-info.java deleted file mode 100644 index 3fec80f..0000000 --- a/src/com/spssol/module/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.module; \ No newline at end of file diff --git a/src/com/spssol/service/package-info.java b/src/com/spssol/service/package-info.java deleted file mode 100644 index 1f67cd5..0000000 --- a/src/com/spssol/service/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.service; \ No newline at end of file diff --git a/src/com/spssol/util/package-info.java b/src/com/spssol/util/package-info.java deleted file mode 100644 index 390a929..0000000 --- a/src/com/spssol/util/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.util; \ No newline at end of file From d2b5e307179706819da3d7d2092424880fb1af0e Mon Sep 17 00:00:00 2001 From: bigtage <896932646@qq.com> Date: Tue, 3 Nov 2015 10:30:21 +0800 Subject: [PATCH 2/3] Signed-off-by: bigtage <896932646@qq.com> --- WebRoot/WEB-INF/web.xml | 44 +- WebRoot/index.jsp | 26 - pom.xml | 148 ++-- src/com/spssol/action/UploadAction.java | 77 ++ src/com/spssol/action/package-info.java | 8 - src/com/spssol/bean/package-info.java | 8 - src/com/spssol/dao/package-info.java | 8 - src/com/spssol/module/package-info.java | 8 - src/com/spssol/service/UploadService.java | 5 + src/com/spssol/service/package-info.java | 8 - src/com/spssol/util/Common.java | 172 ++++ src/com/spssol/util/ExcelUtil.java | 922 ++++++++++++++++++++++ src/com/spssol/util/package-info.java | 8 - src/config/SpringMVC.xml | 99 +++ src/config/log4j.properties | 24 + 15 files changed, 1437 insertions(+), 128 deletions(-) delete mode 100644 WebRoot/index.jsp create mode 100644 src/com/spssol/action/UploadAction.java delete mode 100644 src/com/spssol/action/package-info.java delete mode 100644 src/com/spssol/bean/package-info.java delete mode 100644 src/com/spssol/dao/package-info.java delete mode 100644 src/com/spssol/module/package-info.java create mode 100644 src/com/spssol/service/UploadService.java delete mode 100644 src/com/spssol/service/package-info.java create mode 100644 src/com/spssol/util/Common.java create mode 100644 src/com/spssol/util/ExcelUtil.java delete mode 100644 src/com/spssol/util/package-info.java create mode 100644 src/config/SpringMVC.xml create mode 100644 src/config/log4j.properties diff --git a/WebRoot/WEB-INF/web.xml b/WebRoot/WEB-INF/web.xml index bb1b32c..b9492ce 100644 --- a/WebRoot/WEB-INF/web.xml +++ b/WebRoot/WEB-INF/web.xml @@ -3,10 +3,44 @@ SpssOL index.html - index.htm - index.jsp - default.html - default.htm - default.jsp + + + default + *.html + + + + encoding + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + + + encoding + /* + + + contextConfigLocation + classpath:config/SpringMVC.xml + + + TaMusic + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:config/SpringMVC.xml + + 1 + + + TaMusic + / + + + org.springframework.web.context.ContextLoaderListener + \ No newline at end of file diff --git a/WebRoot/index.jsp b/WebRoot/index.jsp deleted file mode 100644 index 4c4ef27..0000000 --- a/WebRoot/index.jsp +++ /dev/null @@ -1,26 +0,0 @@ -<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> -<% -String path = request.getContextPath(); -String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; -%> - - - - - - - My JSP 'index.jsp' starting page - - - - - - - - - - This is my JSP page.
- - diff --git a/pom.xml b/pom.xml index cedc9e7..b800ba0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,50 +1,100 @@ - - 4.0.0 - SpssOL - SpssOL - 0.0.1-SNAPSHOT - war - SpssOL - - - UTF-8 - - - - javax - javaee-api - 7.0 - provided - - - - src - - - src - - **/*.java - - - - - - maven-compiler-plugin - 2.3.2 - - 1.8 - 1.8 - - - - maven-war-plugin - 2.2 - - ${basedir}/WebRoot - 3.1 - false - - - - + + 4.0.0 + SpssOL + SpssOL + 0.0.1-SNAPSHOT + war + SpssOL + + + UTF-8 + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + org.springframework + spring-orm + 4.2.2.RELEASE + + + org.springframework + spring-webmvc + 4.2.2.RELEASE + + + org.hibernate + hibernate-core + 5.0.2.Final + + + mysql + mysql-connector-java + 5.1.36 + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-base + 2.6.1 + + + commons-fileupload + commons-fileupload + 1.3.1 + + + commons-dbcp + commons-dbcp + 1.4 + + + org + jaudiotagger + 2.0.3 + + + log4j + log4j + 1.2.17 + + + org.apache.poi + poi-ooxml + 3.13 + + + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + maven-war-plugin + 2.2 + + ${basedir}/WebRoot + 3.1 + false + + + + \ No newline at end of file diff --git a/src/com/spssol/action/UploadAction.java b/src/com/spssol/action/UploadAction.java new file mode 100644 index 0000000..676afed --- /dev/null +++ b/src/com/spssol/action/UploadAction.java @@ -0,0 +1,77 @@ +package com.spssol.action; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import com.spssol.service.UploadService; +import com.spssol.util.Common; +import com.spssol.util.ExcelUtil; + +@Controller +@RequestMapping("/file") +public class UploadAction { + + /** + * 上传歌曲 + * + * @param request + * @throws Exception + */ + @RequestMapping(value = "/upload") + @ResponseBody + public Map uploadFile(HttpServletRequest request, + HttpSession session) throws Exception { + Map map = new HashMap(); + + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + Iterator fileNames = multipartRequest.getFileNames(); + while (fileNames.hasNext()) { + MultipartFile multipartFile = multipartRequest.getFile(fileNames + .next()); + // upload路径 + String realPath = request.getSession().getServletContext() + .getRealPath("/upload"); + // 文件名 + String filename = multipartFile.getOriginalFilename(); + // 获取文件的后缀 + String suffix = filename.substring(filename.lastIndexOf(".")); + // 文件MD5 + String md5 = Common.getMD5(multipartFile.getBytes()); + // 保存的路径,文件名保存为MD5值 + String filePath = realPath + "/" + md5 + suffix; + // 判断文件是否存在。若存在则急速秒传 + File file = Common.fileExists(filePath); + if (file != null) { + map.put("jsmc", "极速秒传!"); + } else { + file = Common.saveFileAndReturn(filePath, + multipartFile.getBytes()); + } + if (file != null) { + ExcelUtil eu = new ExcelUtil(); + eu.setExcelPath(filePath); + + System.out.println("=======测试Excel 默认 读取========"); + map.put("data",eu.readExcel()); + System.out.println(map.toString()); + } else { + map.put("status", false); + map.put("msg", "文件保存失败"); + } + } + return map; + } +} \ No newline at end of file diff --git a/src/com/spssol/action/package-info.java b/src/com/spssol/action/package-info.java deleted file mode 100644 index 370c5aa..0000000 --- a/src/com/spssol/action/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.action; \ No newline at end of file diff --git a/src/com/spssol/bean/package-info.java b/src/com/spssol/bean/package-info.java deleted file mode 100644 index 66cc243..0000000 --- a/src/com/spssol/bean/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.bean; \ No newline at end of file diff --git a/src/com/spssol/dao/package-info.java b/src/com/spssol/dao/package-info.java deleted file mode 100644 index 5fa1e9b..0000000 --- a/src/com/spssol/dao/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.dao; \ No newline at end of file diff --git a/src/com/spssol/module/package-info.java b/src/com/spssol/module/package-info.java deleted file mode 100644 index 3fec80f..0000000 --- a/src/com/spssol/module/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.module; \ No newline at end of file diff --git a/src/com/spssol/service/UploadService.java b/src/com/spssol/service/UploadService.java new file mode 100644 index 0000000..d96b26d --- /dev/null +++ b/src/com/spssol/service/UploadService.java @@ -0,0 +1,5 @@ +package com.spssol.service; + +public class UploadService { + +} diff --git a/src/com/spssol/service/package-info.java b/src/com/spssol/service/package-info.java deleted file mode 100644 index 1f67cd5..0000000 --- a/src/com/spssol/service/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.service; \ No newline at end of file diff --git a/src/com/spssol/util/Common.java b/src/com/spssol/util/Common.java new file mode 100644 index 0000000..77714ed --- /dev/null +++ b/src/com/spssol/util/Common.java @@ -0,0 +1,172 @@ +package com.spssol.util; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Common { + /** + * 验证是email格式是否合法 + * + * @param email + * @return 是否合法 + */ + public static boolean isemail(String email) { + boolean tag = true; + final String pattern1 = "^([a-z0-9A-Z]+[-|//.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?//.)+[a-zA-Z]{2,}$"; + final Pattern pattern = Pattern.compile(pattern1); + final Matcher mat = pattern.matcher(email); + if (!mat.find()) { + tag = false; + } + return tag; + } + + /** + * 验证是否为手机号 + * + * @param str + * @return 是否合法 + */ + public static boolean isNumer(String str) { + Pattern pattern = Pattern + .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$"); + return pattern.matcher(str).matches(); + } + + /** + * 判断非空字符串 + * + * @param str + * @return 是否非空 + */ + public static boolean isNull(String str) { + return str != null && str.length() > 0; + } + + /** + * 上传文件,默认路径为upload + * + * @param filePath + * @param content + * @return 返回是否保存成功 + */ + public static boolean saveFile(String filePath, byte[] content) { + boolean flag = true; + BufferedOutputStream bos = null; + try { + File file = new File(filePath); + // 判断文件路径是否存在 + if (!file.getParentFile().exists()) { + // 文件路径不存在时,创建保存文件所需要的路径 + file.getParentFile().mkdirs(); + } + // 创建文件(这是个空文件,用来写入上传过来的文件的内容) + file.createNewFile(); + bos = new BufferedOutputStream(new FileOutputStream(file)); + bos.write(content); + } catch (IOException e) { + e.printStackTrace(); + flag = false; + } finally { + if (null != bos) { + try { + bos.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return flag; + } + + /** + * 上传文件,并返回文件对象 + * + * @param filePath + * @param content + * @return 返回是文件 + */ + public static File saveFileAndReturn(String filePath, byte[] content) { + BufferedOutputStream bos = null; + File file = null; + try { + file = new File(filePath); + // 判断文件路径是否存在 + if (!file.getParentFile().exists()) { + // 文件路径不存在时,创建保存文件所需要的路径 + file.getParentFile().mkdirs(); + } + // 创建文件(这是个空文件,用来写入上传过来的文件的内容) + file.createNewFile(); + bos = new BufferedOutputStream(new FileOutputStream(file)); + bos.write(content); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (null != bos) { + try { + bos.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return file; + } + + /** + * 判断文件是否存在 + * + * @param path + * @return 若存在则返回file对象 + */ + public static File fileExists(String path) { + File file = new File(path); + if (file.exists()) { + return file;// 如果存在输出结果 + } else { + return null; + } + } + + /** + * 获取MD5值 + * + * @param file + * @return + */ + public static String getMD5(byte[] source) { + String s = null; + char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', + 'e', 'f' }; + try { + java.security.MessageDigest md = java.security.MessageDigest + .getInstance("MD5"); + md.update(source); + byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数, + // 用字节表示就是 16 个字节 + char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符, + // 所以表示成 16 进制需要 32 个字符 + int k = 0; // 表示转换结果中对应的字符位置 + for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节 + // 转换成 16 进制字符的转换 + byte byte0 = tmp[i]; // 取第 i 个字节 + str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换, + // >>> 为逻辑右移,将符号位一起右移 + str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换 + } + s = new String(str); // 换后的结果转换为字符串 + + } catch (Exception e) { + e.printStackTrace(); + } + return s; + } +} diff --git a/src/com/spssol/util/ExcelUtil.java b/src/com/spssol/util/ExcelUtil.java new file mode 100644 index 0000000..00f0886 --- /dev/null +++ b/src/com/spssol/util/ExcelUtil.java @@ -0,0 +1,922 @@ +package com.spssol.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +/** + * Excel文件操作工具类,包括读、写、合并等功能 + * + * @author : 龙轩 + * @group : tgb8 + * @Version : 1.00 + * @Date : 2014-10-29 上午12:40:44 + */ +public class ExcelUtil { + + //%%%%%%%%-------常量部分 开始----------%%%%%%%%% + /** + * 默认的开始读取的行位置为第一行(索引值为0) + */ + private final static int READ_START_POS = 0; + + /** + * 默认结束读取的行位置为最后一行(索引值=0,用负数来表示倒数第n行) + */ + private final static int READ_END_POS = 0; + + /** + * 默认Excel内容的开始比较列位置为第一列(索引值为0) + */ + private final static int COMPARE_POS = 0; + + /** + * 默认多文件合并的时需要做内容比较(相同的内容不重复出现) + */ + private final static boolean NEED_COMPARE = true; + + /** + * 默认多文件合并的新文件遇到名称重复时,进行覆盖 + */ + private final static boolean NEED_OVERWRITE = true; + + /** + * 默认只操作一个sheet + */ + private final static boolean ONLY_ONE_SHEET = true; + + /** + * 默认读取第一个sheet中(只有当ONLY_ONE_SHEET = true时有效) + */ + private final static int SELECTED_SHEET = 0; + + /** + * 默认从第一个sheet开始读取(索引值为0) + */ + private final static int READ_START_SHEET= 0; + + /** + * 默认在最后一个sheet结束读取(索引值=0,用负数来表示倒数第n行) + */ + private final static int READ_END_SHEET = 0; + + /** + * 默认打印各种信息 + */ + private final static boolean PRINT_MSG = true; + + //%%%%%%%%-------常量部分 结束----------%%%%%%%%% + + + //%%%%%%%%-------字段部分 开始----------%%%%%%%%% + /** + * Excel文件路径 + */ + private String excelPath = "data.xlsx"; + + /** + * 设定开始读取的位置,默认为0 + */ + private int startReadPos = READ_START_POS; + + /** + * 设定结束读取的位置,默认为0,用负数来表示倒数第n行 + */ + private int endReadPos = READ_END_POS; + + /** + * 设定开始比较的列位置,默认为0 + */ + private int comparePos = COMPARE_POS; + + /** + * 设定汇总的文件是否需要替换,默认为true + */ + private boolean isOverWrite = NEED_OVERWRITE; + + /** + * 设定是否需要比较,默认为true(仅当不覆写目标内容是有效,即isOverWrite=false时有效) + */ + private boolean isNeedCompare = NEED_COMPARE; + + /** + * 设定是否只操作第一个sheet + */ + private boolean onlyReadOneSheet = ONLY_ONE_SHEET; + + /** + * 设定操作的sheet在索引值 + */ + private int selectedSheetIdx =SELECTED_SHEET; + + /** + * 设定操作的sheet的名称 + */ + private String selectedSheetName = ""; + + /** + * 设定开始读取的sheet,默认为0 + */ + private int startSheetIdx = READ_START_SHEET; + + /** + * 设定结束读取的sheet,默认为0,用负数来表示倒数第n行 + */ + private int endSheetIdx = READ_END_SHEET; + + /** + * 设定是否打印消息 + */ + private boolean printMsg = PRINT_MSG; + + + //%%%%%%%%-------字段部分 结束----------%%%%%%%%% + + public ExcelUtil(){ + + } + + public ExcelUtil(String excelPath){ + this.excelPath = excelPath; + } + + /** + * 还原设定(其实是重新new一个新的对象并返回) + * @return + */ + public ExcelUtil RestoreSettings(){ + ExcelUtil instance = new ExcelUtil(this.excelPath); + return instance; + } + + /** + * 自动根据文件扩展名,调用对应的读取方法 + * + * @Title: writeExcel + * @Date : 2014-9-11 下午01:50:38 + * @param xlsPath + * @throws IOException + */ + public List readExcel() throws IOException{ + return readExcel(this.excelPath); + } + + /** + * 自动根据文件扩展名,调用对应的读取方法 + * + * @Title: writeExcel + * @Date : 2014-9-11 下午01:50:38 + * @param xlsPath + * @throws IOException + */ + public List readExcel(String xlsPath) throws IOException{ + + //扩展名为空时, + if (xlsPath.equals("")){ + throw new IOException("文件路径不能为空!"); + }else{ + File file = new File(xlsPath); + if(!file.exists()){ + throw new IOException("文件不存在!"); + } + } + + //获取扩展名 + String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1); + + try { + + if("xls".equals(ext)){ //使用xls方式读取 + return readExcel_xls(xlsPath); + }else if("xlsx".equals(ext)){ //使用xlsx方式读取 + return readExcel_xlsx(xlsPath); + }else{ //依次尝试xls、xlsx方式读取 + out("您要操作的文件没有扩展名,正在尝试以xls方式读取..."); + try{ + return readExcel_xls(xlsPath); + } catch (IOException e1) { + out("尝试以xls方式读取,结果失败!,正在尝试以xlsx方式读取..."); + try{ + return readExcel_xlsx(xlsPath); + } catch (IOException e2) { + out("尝试以xls方式读取,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。"); + throw e2; + } + } + } + } catch (IOException e) { + throw e; + } + } + + /** + * 自动根据文件扩展名,调用对应的写入方法 + * + * @Title: writeExcel + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @throws IOException + */ + public void writeExcel(List rowList) throws IOException{ + writeExcel(rowList,excelPath); + } + + /** + * 自动根据文件扩展名,调用对应的写入方法 + * + * @Title: writeExcel + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @param xlsPath + * @throws IOException + */ + public void writeExcel(List rowList, String xlsPath) throws IOException { + + //扩展名为空时, + if (xlsPath.equals("")){ + throw new IOException("文件路径不能为空!"); + } + + //获取扩展名 + String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1); + + try { + + if("xls".equals(ext)){ //使用xls方式写入 + writeExcel_xls(rowList,xlsPath); + }else if("xlsx".equals(ext)){ //使用xlsx方式写入 + writeExcel_xlsx(rowList,xlsPath); + }else{ //依次尝试xls、xlsx方式写入 + out("您要操作的文件没有扩展名,正在尝试以xls方式写入..."); + try{ + writeExcel_xls(rowList,xlsPath); + } catch (IOException e1) { + out("尝试以xls方式写入,结果失败!,正在尝试以xlsx方式读取..."); + try{ + writeExcel_xlsx(rowList,xlsPath); + } catch (IOException e2) { + out("尝试以xls方式写入,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。"); + throw e2; + } + } + } + } catch (IOException e) { + throw e; + } + } + + /** + * 修改Excel(97-03版,xls格式) + * + * @Title: writeExcel_xls + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @param dist_xlsPath + * @throws IOException + */ + public void writeExcel_xls(List rowList, String dist_xlsPath) throws IOException { + writeExcel_xls(rowList, excelPath,dist_xlsPath); + } + + /** + * 修改Excel(97-03版,xls格式) + * + * @Title: writeExcel_xls + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @param src_xlsPath + * @param dist_xlsPath + * @throws IOException + */ + public void writeExcel_xls(List rowList, String src_xlsPath, String dist_xlsPath) throws IOException { + + // 判断文件路径是否为空 + if (dist_xlsPath == null || dist_xlsPath.equals("")) { + out("文件路径不能为空"); + throw new IOException("文件路径不能为空"); + } + // 判断文件路径是否为空 + if (src_xlsPath == null || src_xlsPath.equals("")) { + out("文件路径不能为空"); + throw new IOException("文件路径不能为空"); + } + + // 判断列表是否有数据,如果没有数据,则返回 + if (rowList == null || rowList.size() == 0) { + out("文档为空"); + return; + } + + try { + HSSFWorkbook wb = null; + + // 判断文件是否存在 + File file = new File(dist_xlsPath); + if (file.exists()) { + // 如果复写,则删除后 + if (isOverWrite) { + file.delete(); + // 如果文件不存在,则创建一个新的Excel + // wb = new HSSFWorkbook(); + // wb.createSheet("Sheet1"); + wb = new HSSFWorkbook(new FileInputStream(src_xlsPath)); + } else { + // 如果文件存在,则读取Excel + wb = new HSSFWorkbook(new FileInputStream(file)); + } + } else { + // 如果文件不存在,则创建一个新的Excel + // wb = new HSSFWorkbook(); + // wb.createSheet("Sheet1"); + wb = new HSSFWorkbook(new FileInputStream(src_xlsPath)); + } + + // 将rowlist的内容写到Excel中 + writeExcel(wb, rowList, dist_xlsPath); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 修改Excel(97-03版,xls格式) + * + * @Title: writeExcel_xls + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @param dist_xlsPath + * @throws IOException + */ + public void writeExcel_xlsx(List rowList, String dist_xlsPath) throws IOException { + writeExcel_xls(rowList, excelPath , dist_xlsPath); + } + + /** + * 修改Excel(2007版,xlsx格式) + * + * @Title: writeExcel_xlsx + * @Date : 2014-9-11 下午01:50:38 + * @param rowList + * @param xlsPath + * @throws IOException + */ + public void writeExcel_xlsx(List rowList, String src_xlsPath, String dist_xlsPath) throws IOException { + + // 判断文件路径是否为空 + if (dist_xlsPath == null || dist_xlsPath.equals("")) { + out("文件路径不能为空"); + throw new IOException("文件路径不能为空"); + } + // 判断文件路径是否为空 + if (src_xlsPath == null || src_xlsPath.equals("")) { + out("文件路径不能为空"); + throw new IOException("文件路径不能为空"); + } + + // 判断列表是否有数据,如果没有数据,则返回 + if (rowList == null || rowList.size() == 0) { + out("文档为空"); + return; + } + + try { + // 读取文档 + XSSFWorkbook wb = null; + + // 判断文件是否存在 + File file = new File(dist_xlsPath); + if (file.exists()) { + // 如果复写,则删除后 + if (isOverWrite) { + file.delete(); + // 如果文件不存在,则创建一个新的Excel + // wb = new XSSFWorkbook(); + // wb.createSheet("Sheet1"); + wb = new XSSFWorkbook(new FileInputStream(src_xlsPath)); + } else { + // 如果文件存在,则读取Excel + wb = new XSSFWorkbook(new FileInputStream(file)); + } + } else { + // 如果文件不存在,则创建一个新的Excel + // wb = new XSSFWorkbook(); + // wb.createSheet("Sheet1"); + wb = new XSSFWorkbook(new FileInputStream(src_xlsPath)); + } + // 将rowlist的内容添加到Excel中 + writeExcel(wb, rowList, dist_xlsPath); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * //读取Excel 2007版,xlsx格式 + * + * @Title: readExcel_xlsx + * @Date : 2014-9-11 上午11:43:11 + * @return + * @throws IOException + */ + public List readExcel_xlsx() throws IOException { + return readExcel_xlsx(excelPath); + } + + /** + * //读取Excel 2007版,xlsx格式 + * + * @Title: readExcel_xlsx + * @Date : 2014-9-11 上午11:43:11 + * @return + * @throws Exception + */ + public List readExcel_xlsx(String xlsPath) throws IOException { + // 判断文件是否存在 + File file = new File(xlsPath); + if (!file.exists()) { + throw new IOException("文件名为" + file.getName() + "Excel文件不存在!"); + } + + XSSFWorkbook wb = null; + List rowList = new ArrayList(); + try { + FileInputStream fis = new FileInputStream(file); + // 去读Excel + wb = new XSSFWorkbook(fis); + + // 读取Excel 2007版,xlsx格式 + rowList = readExcel(wb); + + } catch (IOException e) { + e.printStackTrace(); + } + return rowList; + } + + /*** + * 读取Excel(97-03版,xls格式) + * + * @throws IOException + * + * @Title: readExcel + * @Date : 2014-9-11 上午09:53:21 + */ + public List readExcel_xls() throws IOException { + return readExcel_xls(excelPath); + } + + /*** + * 读取Excel(97-03版,xls格式) + * + * @throws Exception + * + * @Title: readExcel + * @Date : 2014-9-11 上午09:53:21 + */ + public List readExcel_xls(String xlsPath) throws IOException { + + // 判断文件是否存在 + File file = new File(xlsPath); + if (!file.exists()) { + throw new IOException("文件名为" + file.getName() + "Excel文件不存在!"); + } + + HSSFWorkbook wb = null;// 用于Workbook级的操作,创建、删除Excel + List rowList = new ArrayList(); + + try { + // 读取Excel + wb = new HSSFWorkbook(new FileInputStream(file)); + + // 读取Excel 97-03版,xls格式 + rowList = readExcel(wb); + + } catch (IOException e) { + e.printStackTrace(); + } + return rowList; + } + + /*** + * 读取单元格的值 + * + * @Title: getCellValue + * @Date : 2014-9-11 上午10:52:07 + * @param cell + * @return + */ + private String getCellValue(Cell cell) { + Object result = ""; + if (cell != null) { + switch (cell.getCellType()) { + case Cell.CELL_TYPE_STRING: + result = cell.getStringCellValue(); + break; + case Cell.CELL_TYPE_NUMERIC: + result = cell.getNumericCellValue(); + break; + case Cell.CELL_TYPE_BOOLEAN: + result = cell.getBooleanCellValue(); + break; + case Cell.CELL_TYPE_FORMULA: + result = cell.getCellFormula(); + break; + case Cell.CELL_TYPE_ERROR: + result = cell.getErrorCellValue(); + break; + case Cell.CELL_TYPE_BLANK: + break; + default: + break; + } + } + return result.toString(); + } + + /** + * 通用读取Excel + * + * @Title: readExcel + * @Date : 2014-9-11 上午11:26:53 + * @param wb + * @return + */ + private List readExcel(Workbook wb) { + List rowList = new ArrayList(); + + int sheetCount = 1;//需要操作的sheet数量 + + Sheet sheet = null; + if(onlyReadOneSheet){ //只操作一个sheet + // 获取设定操作的sheet(如果设定了名称,按名称查,否则按索引值查) + sheet =selectedSheetName.equals("")? wb.getSheetAt(selectedSheetIdx):wb.getSheet(selectedSheetName); + }else{ //操作多个sheet + sheetCount = wb.getNumberOfSheets();//获取可以操作的总数量 + } + + // 获取sheet数目 + for(int t=startSheetIdx; t0){ //如果>0,表示有数据 + out("\n开始读取名为【"+sheet.getSheetName()+"】的内容:"); + } + + Row row = null; + // 循环读取 + for (int i = startReadPos; i <= lastRowNum + endReadPos; i++) { + row = sheet.getRow(i); + if (row != null) { + rowList.add(row); + out("第"+(i+1)+"行:",false); + // 获取每一单元格的值 + for (int j = 0; j < row.getLastCellNum(); j++) { + String value = getCellValue(row.getCell(j)); + if (!value.equals("")) { + out(value + " | ",false); + } + } + out(""); + } + } + } + return rowList; + } + + /** + * 修改Excel,并另存为 + * + * @Title: WriteExcel + * @Date : 2014-9-11 下午01:33:59 + * @param wb + * @param rowList + * @param xlsPath + */ + private void writeExcel(Workbook wb, List rowList, String xlsPath) { + + if (wb == null) { + out("操作文档不能为空!"); + return; + } + + Sheet sheet = wb.getSheetAt(0);// 修改第一个sheet中的值 + + // 如果每次重写,那么则从开始读取的位置写,否则果获取源文件最新的行。 + int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1; + int t = 0;//记录最新添加的行数 + out("要添加的数据总条数为:"+rowList.size()); + for (Row row : rowList) { + if (row == null) continue; + // 判断是否已经存在该数据 + int pos = findInExcel(sheet, row); + + Row r = null;// 如果数据行已经存在,则获取后重写,否则自动创建新行。 + if (pos >= 0) { + sheet.removeRow(sheet.getRow(pos)); + r = sheet.createRow(pos); + } else { + r = sheet.createRow(lastRowNum + t++); + } + + //用于设定单元格样式 + CellStyle newstyle = wb.createCellStyle(); + + //循环为新行创建单元格 + for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) { + Cell cell = r.createCell(i);// 获取数据类型 + cell.setCellValue(getCellValue(row.getCell(i)));// 复制单元格的值到新的单元格 + // cell.setCellStyle(row.getCell(i).getCellStyle());//出错 + if (row.getCell(i) == null) continue; + copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // 获取原来的单元格样式 + cell.setCellStyle(newstyle);// 设置样式 + // sheet.autoSizeColumn(i);//自动跳转列宽度 + } + } + out("其中检测到重复条数为:" + (rowList.size() - t) + " ,追加条数为:"+t); + + // 统一设定合并单元格 + setMergedRegion(sheet); + + try { + // 重新将数据写入Excel中 + FileOutputStream outputStream = new FileOutputStream(xlsPath); + wb.write(outputStream); + outputStream.flush(); + outputStream.close(); + } catch (Exception e) { + out("写入Excel时发生错误! "); + e.printStackTrace(); + } + } + + /** + * 查找某行数据是否在Excel表中存在,返回行数。 + * + * @Title: findInExcel + * @Date : 2014-9-11 下午02:23:12 + * @param sheet + * @param row + * @return + */ + private int findInExcel(Sheet sheet, Row row) { + int pos = -1; + + try { + // 如果覆写目标文件,或者不需要比较,则直接返回 + if (isOverWrite || !isNeedCompare) { + return pos; + } + for (int i = startReadPos; i <= sheet.getLastRowNum() + endReadPos; i++) { + Row r = sheet.getRow(i); + if (r != null && row != null) { + String v1 = getCellValue(r.getCell(comparePos)); + String v2 = getCellValue(row.getCell(comparePos)); + if (v1.equals(v2)) { + pos = i; + break; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return pos; + } + + /** + * 复制一个单元格样式到目的单元格样式 + * + * @param fromStyle + * @param toStyle + */ + public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) { + toStyle.setAlignment(fromStyle.getAlignment()); + // 边框和边框颜色 + toStyle.setBorderBottom(fromStyle.getBorderBottom()); + toStyle.setBorderLeft(fromStyle.getBorderLeft()); + toStyle.setBorderRight(fromStyle.getBorderRight()); + toStyle.setBorderTop(fromStyle.getBorderTop()); + toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); + toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); + toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); + toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); + + // 背景和前景 + toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); + toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); + + // 数据格式 + toStyle.setDataFormat(fromStyle.getDataFormat()); + toStyle.setFillPattern(fromStyle.getFillPattern()); + // toStyle.setFont(fromStyle.getFont(null)); + toStyle.setHidden(fromStyle.getHidden()); + toStyle.setIndention(fromStyle.getIndention());// 首行缩进 + toStyle.setLocked(fromStyle.getLocked()); + toStyle.setRotation(fromStyle.getRotation());// 旋转 + toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); + toStyle.setWrapText(fromStyle.getWrapText()); + + } + + /** + * 获取合并单元格的值 + * + * @param sheet + * @param row + * @param column + * @return + */ + public void setMergedRegion(Sheet sheet) { + int sheetMergeCount = sheet.getNumMergedRegions(); + + for (int i = 0; i < sheetMergeCount; i++) { + // 获取合并单元格位置 + CellRangeAddress ca = sheet.getMergedRegion(i); + int firstRow = ca.getFirstRow(); + if (startReadPos - 1 > firstRow) {// 如果第一个合并单元格格式在正式数据的上面,则跳过。 + continue; + } + int lastRow = ca.getLastRow(); + int mergeRows = lastRow - firstRow;// 合并的行数 + int firstColumn = ca.getFirstColumn(); + int lastColumn = ca.getLastColumn(); + // 根据合并的单元格位置和大小,调整所有的数据行格式, + for (int j = lastRow + 1; j <= sheet.getLastRowNum(); j++) { + // 设定合并单元格 + sheet.addMergedRegion(new CellRangeAddress(j, j + mergeRows, firstColumn, lastColumn)); + j = j + mergeRows;// 跳过已合并的行 + } + + } + } + + + /** + * 打印消息, + * @param msg 消息内容 + * @param tr 换行 + */ + private void out(String msg){ + if(printMsg){ + out(msg,true); + } + } + /** + * 打印消息, + * @param msg 消息内容 + * @param tr 换行 + */ + private void out(String msg,boolean tr){ + if(printMsg){ + System.out.print(msg+(tr?"\n":"")); + } + } + + public String getExcelPath() { + return this.excelPath; + } + + public void setExcelPath(String excelPath) { + this.excelPath = excelPath; + } + + public boolean isNeedCompare() { + return isNeedCompare; + } + + public void setNeedCompare(boolean isNeedCompare) { + this.isNeedCompare = isNeedCompare; + } + + public int getComparePos() { + return comparePos; + } + + public void setComparePos(int comparePos) { + this.comparePos = comparePos; + } + + public int getStartReadPos() { + return startReadPos; + } + + public void setStartReadPos(int startReadPos) { + this.startReadPos = startReadPos; + } + + public int getEndReadPos() { + return endReadPos; + } + + public void setEndReadPos(int endReadPos) { + this.endReadPos = endReadPos; + } + + public boolean isOverWrite() { + return isOverWrite; + } + + public void setOverWrite(boolean isOverWrite) { + this.isOverWrite = isOverWrite; + } + + public boolean isOnlyReadOneSheet() { + return onlyReadOneSheet; + } + + public void setOnlyReadOneSheet(boolean onlyReadOneSheet) { + this.onlyReadOneSheet = onlyReadOneSheet; + } + + public int getSelectedSheetIdx() { + return selectedSheetIdx; + } + + public void setSelectedSheetIdx(int selectedSheetIdx) { + this.selectedSheetIdx = selectedSheetIdx; + } + + public String getSelectedSheetName() { + return selectedSheetName; + } + + public void setSelectedSheetName(String selectedSheetName) { + this.selectedSheetName = selectedSheetName; + } + + public int getStartSheetIdx() { + return startSheetIdx; + } + + public void setStartSheetIdx(int startSheetIdx) { + this.startSheetIdx = startSheetIdx; + } + + public int getEndSheetIdx() { + return endSheetIdx; + } + + public void setEndSheetIdx(int endSheetIdx) { + this.endSheetIdx = endSheetIdx; + } + + public boolean isPrintMsg() { + return printMsg; + } + + public void setPrintMsg(boolean printMsg) { + this.printMsg = printMsg; + } + + public static void main(String[] args) { + try { + + ExcelUtil eu = new ExcelUtil(); + eu.setExcelPath("C:/Users/bigta/Desktop/65岁和65岁以上的人口(占总人口的百分比).xls"); + + System.out.println("=======测试Excel 默认 读取========"); + List list =eu.readExcel(); + +// +// System.out.println("\n=======测试Excel 从第四行读取,倒数第二行结束========"); +// eu = eu.RestoreSettings();//还原设定 +// eu.setStartReadPos(3); +// eu.setEndReadPos(-1); +// eu.readExcel(); +// +// System.out.println("\n=======测试Excel 读取第二个sheet========"); +// eu = eu.RestoreSettings();//还原设定 +// eu.setSelectedSheetIdx(1); +// eu.readExcel(); +// +// System.out.println("\n=======测试Excel 读取所有的sheet========"); +// eu = eu.RestoreSettings();//还原设定 +// eu.setOnlyReadOneSheet(false); +// eu.readExcel(); +// + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/com/spssol/util/package-info.java b/src/com/spssol/util/package-info.java deleted file mode 100644 index 390a929..0000000 --- a/src/com/spssol/util/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author bigta - * - */ -package com.spssol.util; \ No newline at end of file diff --git a/src/config/SpringMVC.xml b/src/config/SpringMVC.xml new file mode 100644 index 0000000..c551ab0 --- /dev/null +++ b/src/config/SpringMVC.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.hibernate.dialect.MySQLDialect + + true + update + true + thread + + + + + com.bigtage.bean + + + + + + + + + + + + + + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED,readOnly + PROPAGATION_REQUIRED,readOnly + PROPAGATION_REQUIRED,readOnly + PROPAGATION_REQUIRED,readOnly + + + + \ No newline at end of file diff --git a/src/config/log4j.properties b/src/config/log4j.properties new file mode 100644 index 0000000..a3056b2 --- /dev/null +++ b/src/config/log4j.properties @@ -0,0 +1,24 @@ +### \u8BBE\u7F6E### +log4j.rootLogger = debug,stdout,D,E + +### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ### +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target = System.out +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n + +### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ### +log4j.appender.D = org.apache.log4j.DailyRollingFileAppender +log4j.appender.D.File = E://logs/log.log +log4j.appender.D.Append = true +log4j.appender.D.Threshold = DEBUG +log4j.appender.D.layout = org.apache.log4j.PatternLayout +log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n + +### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ### +log4j.appender.E = org.apache.log4j.DailyRollingFileAppender +log4j.appender.E.File =E://logs/error.log +log4j.appender.E.Append = true +log4j.appender.E.Threshold = ERROR +log4j.appender.E.layout = org.apache.log4j.PatternLayout +log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n \ No newline at end of file From 5e4cba2ab4bcf424330e3d1047f97187bf319c09 Mon Sep 17 00:00:00 2001 From: bigtage <896932646@qq.com> Date: Tue, 3 Nov 2015 11:55:14 +0800 Subject: [PATCH 3/3] Signed-off-by: bigtage <896932646@qq.com> --- src/com/spssol/action/UploadAction.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/com/spssol/action/UploadAction.java b/src/com/spssol/action/UploadAction.java index 676afed..13b7fba 100644 --- a/src/com/spssol/action/UploadAction.java +++ b/src/com/spssol/action/UploadAction.java @@ -5,18 +5,15 @@ import java.util.Iterator; import java.util.Map; -import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; -import com.spssol.service.UploadService; import com.spssol.util.Common; import com.spssol.util.ExcelUtil; @@ -35,7 +32,7 @@ public class UploadAction { public Map uploadFile(HttpServletRequest request, HttpSession session) throws Exception { Map map = new HashMap(); - +System.out.println("123"); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Iterator fileNames = multipartRequest.getFileNames(); while (fileNames.hasNext()) { @@ -66,7 +63,6 @@ public Map uploadFile(HttpServletRequest request, System.out.println("=======测试Excel 默认 读取========"); map.put("data",eu.readExcel()); - System.out.println(map.toString()); } else { map.put("status", false); map.put("msg", "文件保存失败");