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 getReadFileInfo(FileItem file) throws MINBusinessException { return getReadFileInfo(file, ""); } /** * 读取text、csv等文件 * @param file * @param code 编码格式 * @return * @throws MINBusinessException */ public static List getReadFileInfo(FileItem file,String code)throws MINBusinessException { List list = new ArrayList(); 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> readExecl(FileItem execl,String[] titleKey,int firstrow) throws InvalidFormatException, IOException, MINBusinessException{ List> list = new ArrayList>(); 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 map = new HashMap(); for(int j=0; j> readExecleasy(FileItem execl,String[] titleKey,int firstrow) throws InvalidFormatException, IOException, MINBusinessException{ List> list = new ArrayList>(); 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 map = new HashMap(); for(int j=0; j