| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- package com.minpay.common.util;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- import org.apache.poi.openxml4j.util.ZipSecureFile;
- import org.apache.poi.ss.usermodel.Cell;
- 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.usermodel.WorkbookFactory;
- import com.minpay.common.constant.Constant;
- import com.minpay.common.exception.BusinessCodeException;
- import com.startup.minpay.frame.exception.MINBusinessException;
- import com.startup.minpay.util.Log;
- /**
- * 文件处理工具类
- * @author sunbz
- *
- */
- public class FilesUtil {
- /**
- * 文件上传到指定路径下
- * @param file
- * @param basePath 根路径(文件服务器路径例如F:/Files、/home/Files)
- * @param savePath 子路径,用于根路径下子文件夹
- * @param saveFileName 保存文件名
- * @param size 字节流缓存区大小
- * @throws MINBusinessException
- */
- public static String uploadFile(FileItem file,String basePath,String savePath,String saveFileName,int size) throws IOException, MINBusinessException{
- String url = "";
- int numberRead = 0;
- if(size == 0){
- size = 20000;
- }
- byte[] buffer=new byte[size];//缓冲区
- InputStream in = null;
- FileOutputStream out = null;
- System.out.println("getContentType "+file.getContentType());
- try {
- String fileName = file.getName();
- if(fileName.indexOf("\\")!=-1){
- int le = fileName.lastIndexOf("\\");
- fileName = fileName.substring(le+1, fileName.length());
- System.out.println("saveFileName "+fileName);
- }
- in = file.getInputStream();
- if(saveFileName==null ||"".equals(saveFileName)){
- saveFileName = fileName ;
- }else{
- if(saveFileName.indexOf(".")!=-1){
- throw new MINBusinessException("文件名称格式异常,请检查");
- }
- }
- //处理路径问题 判断是否存在,不存在则创建
- if (!(new File(basePath + savePath).isDirectory())) {
- new File(basePath + savePath).mkdir();
- }
- System.out.println("上传路径:"+basePath + savePath + saveFileName);
- out = new FileOutputStream( basePath + savePath + saveFileName);
- while ((numberRead=in.read(buffer))!=-1) { //numberRead的目的在于防止最后一次读取的字节小于buffer长度,
- out.write(buffer, 0, numberRead); //否则会自动被填充0
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- in.close();
- out.close();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- url = Constant.FILE_SERVER_PATH + savePath + saveFileName;
- return url;
- }
-
- /**
- * 读取txt、csv等文件
- */
- public static List<String> getReadFileInfo(FileItem file)
- throws MINBusinessException {
- return getReadFileInfo(file, "");
- }
- /**
- * 读取text、csv等文件
- * @param file
- * @param code 编码格式
- * @return
- * @throws MINBusinessException
- */
- public static List<String> getReadFileInfo(FileItem file,String code)throws MINBusinessException {
- List<String> list = new ArrayList<String>();
- if(file==null || file.getSize()<=0){
- throw new MINBusinessException("请选择文件");
- }
- try {
- BufferedReader in = null;
- String line = "";
- if("".equals(code)){
- String[] array = {"GBK","UTF-8","ISO-8859-1","ASCII","GB2312","Big5","Unicode"};
- for (int i = 0; i < array .length; i++) {
- in = new BufferedReader(new InputStreamReader(file.getInputStream(),array[i]));
- line = in.readLine();
- BufferedReader in1 = new BufferedReader(new InputStreamReader(file.getInputStream(),array[i]));
- String line1 = in1.readLine();
- boolean isMessyCode_flag=false;
- while(line1 != null){
- if(isMessyCode(line1)){
- isMessyCode_flag=true;
- break;
- }
- line1 = in1.readLine();
- //测试是否为乱码,否就退出循环
- }
- if(!isMessyCode_flag){
- break;
- }
- }
- }else{
- in = new BufferedReader(new InputStreamReader(file.getInputStream(),code));
- line = in.readLine();
- if("UTF-8".equalsIgnoreCase(code)){
- if(line.startsWith("\uFEFF")){
- line = line.replace("\uFEFF", "");
- }
- }
- //测试是否为乱码,否就退出循环
- if(isMessyCode(line)){
- throw new MINBusinessException("文件编码错误");
- }
- }
- while (line != null) {
- list.add(line);
- line = in.readLine();
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw new MINBusinessException("导入文件内容不正确");
- }
- return list;
- }
- public static boolean isChinese(char c) {
- Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
- if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
- || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
- || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
- || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
- || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
- || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
- return true;
- }
- return false;
- }
-
-
- /**
- * 根据地址获得数据的字节流
- * @param strUrl 网络连接地址
- * @return
- */
- public static byte[] getVMFromNetByUrl(String strUrl){
- try {
- URL url = new URL(strUrl);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5 * 1000);
- InputStream inStream = conn.getInputStream();//通过输入流获取数据
- byte[] btImg = readInputStream(inStream);//得到的二进制数据
- return btImg;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * 从输入流中获取数据
- * @param inStream 输入流
- * @return
- * @throws Exception
- */
- public static byte[] readInputStream(InputStream inStream) throws Exception{
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len=inStream.read(buffer)) != -1 ){
- outStream.write(buffer, 0, len);
- }
- inStream.close();
- return outStream.toByteArray();
- }
-
- public static boolean isMessyCode(String strName) {
- Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
- Matcher m = p.matcher(strName);
- String after = m.replaceAll("");
- String temp = after.replaceAll("\\p{P}", "").replaceAll("\\|", "");
- char[] ch = temp.trim().toCharArray();
- float chLength = ch.length;
- float count = 0;
- for (int i = 0; i < ch.length; i++) {
- char c = ch[i];
- if (!Character.isLetterOrDigit(c)) {
-
- if (!isChinese(c)) {
- count = count + 1;
- System.out.print(c);
- }
- }
- }
- float result = count / chLength;
- if (count > 0) {
- return true;
- }else{
- return false;
- }
- }
- /**
- * 读取xlsx格式Execl文档,暂不支持xls格式
- * @param execl 文件
- * @param titleKey map中存放key,与execl中每列对应
- * @param firstrow 读取开始行
- * @return
- * @throws InvalidFormatException
- * @throws IOException
- * @throws MINBusinessException
- */
- public static List<Map<String,Object>> readExecl(FileItem execl,String[] titleKey,int firstrow) throws InvalidFormatException, IOException, MINBusinessException{
-
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
- Workbook wb = WorkbookFactory.create(execl.getInputStream());
- /** 得到第一个shell */
- Sheet sheet = wb.getSheetAt(0);
- Row firstRow = sheet.getRow(0);
- if(titleKey.length != firstRow.getPhysicalNumberOfCells()){
- throw new BusinessCodeException("PBIM2800");//文件数据格式不正确
- }
-
- int rownum = sheet.getPhysicalNumberOfRows();
- //获取最后一行的下标
- int lastNum = sheet.getLastRowNum();
- Log.info("物理行数 "+sheet.getLastRowNum()+" lastNum:"+lastNum);
- int cellnum = firstRow.getLastCellNum();
- if(cellnum>titleKey.length){
- throw new MINBusinessException("PBIM2800");//提示数据格式不正确
- }
- DecimalFormat df = new DecimalFormat("#");
-
- for(int i=firstrow; i<=lastNum; i++){
- //用来判断当前行所有单元格是否为空
- int count = 0;
- Row row = sheet.getRow(i);
- if(row == null){
- continue;
- }
- Map<String,Object> map = new HashMap<String,Object>();
- for(int j=0; j<titleKey.length; j++){
- System.out.println(i+":"+j);
- Cell cell = row.getCell(j);
- String cellValue="";
- if(cell != null){
-
- switch (cell.getCellType()){
- case NUMERIC: // 数字
- cellValue = df.format(cell.getNumericCellValue()); //cell.getNumericCellValue() + "";
- break;
- case STRING: // 字符串
- cellValue = cell.getStringCellValue();
- break;
- case BOOLEAN: // Boolean
- cellValue = cell.getBooleanCellValue() + "";
- break;
- case FORMULA: // 公式
- cellValue = cell.getCellFormula() + "";
- break;
- case BLANK: // 空值
- cellValue = "";
- break;
- case ERROR: // 故障
- cellValue = "非法字符";
- break;
- default:
- cellValue = "未知类型";
- break;
- }
- }
- if("".equals(cellValue) || cellValue == null){
- count ++;
- }
- map.put(titleKey[j], cellValue);
- }
- if(count != titleKey.length){
- map.put("currentLine", i+1);
- list.add(map);
- }
- }
-
- return list;
-
- }
-
- /**
- * 读取xlsx格式Execl文档,暂不支持xls格式 简版
- * @param execl 文件
- * @param titleKey map中存放key,与execl中每列对应
- * @param firstrow 读取开始行
- * @return
- * @throws InvalidFormatException
- * @throws IOException
- * @throws MINBusinessException
- */
- public static List<Map<String,Object>> readExecleasy(FileItem execl,String[] titleKey,int firstrow) throws InvalidFormatException, IOException, MINBusinessException{
-
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
- ZipSecureFile.setMinInflateRatio(-1.0d);
- Workbook wb = WorkbookFactory.create(execl.getInputStream());
- /** 得到第一个shell */
- Sheet sheet = wb.getSheetAt(0);
- Row firstRow = sheet.getRow(0);
-
- int rownum = sheet.getPhysicalNumberOfRows();
- System.out.println("getContentType "+sheet.getPhysicalNumberOfRows()+" rownum:"+rownum);
- //判断行数是否为0
- if(rownum == 0){
- throw new BusinessCodeException("PBIM2800");//提示数据格式不正确
- }
- // int cellnum = firstRow.getLastCellNum();
- /*if(cellnum>titleKey.length){
- throw new BusinessCodeException("PBIM2800");//提示数据格式不正确
- }*/
- DecimalFormat df = new DecimalFormat("###.####");
-
- for(int i=firstrow; i<rownum; i++){
- Row row = sheet.getRow(i);
- if(row != null) {
- Map<String,Object> map = new HashMap<String,Object>();
- for(int j=0; j<titleKey.length; j++){
- System.out.println(i+":"+j);
- Cell cell = row.getCell(j);
- String cellValue="";
- if(cell != null){
- switch (cell.getCellType()){
- case NUMERIC: // 数字
- if(HSSFDateUtil.isCellDateFormatted(cell)){
- //用于转化为日期格式
- Date d = cell.getDateCellValue();
- DateFormat formater = new SimpleDateFormat("yyyyMMdd");
- cellValue = formater.format(d);
- }else{
- // 用于格式化数字,只保留数字的整数部分
- cellValue = df.format(cell.getNumericCellValue()); //cell.getNumericCellValue() + "";
- }
- break;
- case STRING: // 字符串
- cellValue = cell.getStringCellValue();
- break;
- case BOOLEAN: // Boolean
- cellValue = cell.getBooleanCellValue() + "";
- break;
- case FORMULA: // 公式
- cellValue = cell.getCellFormula() + "";
- break;
- case BLANK: // 空值
- cellValue = "";
- break;
- case ERROR: // 故障
- cellValue = "非法字符";
- break;
- default:
- cellValue = "未知类型";
- break;
- }
- }
- map.put(titleKey[j], cellValue);
- }
- list.add(map);
- }
- }
- return list;
- }
-
- public static void writeFile2Response(HttpServletResponse response, String filUrl) {
- String fileName = filUrl.split("\\/")[filUrl.split("\\/").length - 1];
- try {
- fileName = URLEncoder.encode(fileName, "UTF-8");
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- }
- response.setContentType("application/x-download;charset=UTF-8");
- response.addHeader("Content-disposition", "filename=" + fileName);
- OutputStream os = null;
- ServletOutputStream ros = null;
- InputStream inStream = null;
- try {
- ros = response.getOutputStream();
- os = new BufferedOutputStream(ros);
- // 获取输入流
- URL url = new URL(filUrl);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(120 * 1000);
- response.addHeader("Content-Length", "" + conn.getContentLength());
- inStream = conn.getInputStream();//通过输入流获取数据
- // 新的 byte 数组输出流,缓冲区容量1024byte
- ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- byte[] bs = new byte[1024];
- int n;
- while ((n = inStream.read(bs)) != -1) {
- bos.write(bs, 0, n);
- }
- // 改变为byte[]
- byte[] data = bos.toByteArray();
- bos.close();
- os.write(data);
- os.flush();
- ros.flush();
- } catch (IOException e) {
- Log.error("意外的异常:", e);
- } finally {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- Log.error("意外的异常:", e);
- }
- }
- if (ros != null) {
- try {
- ros.flush();
- ros.close();
- } catch (IOException e) {
- Log.error("意外的异常:", e);
- }
- }
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- Log.error("意外的异常:", e);
- }
- }
- }
- }
- }
|