//实现AutoCloseable接口 不用去关心各种流的关闭。 implements AutoCloseable
@Value("${file.upload}") private String FileUpload;
//返回一个txt文本的url public SRSP getTxtUrl(Set<String> registeridList) { SRSP srsp=new SRSP(); if (registeridList==null || registeridList.isEmpty()){ srsp.setFriendErrMsg("上传失败,读取到的手机号码列表为空"); srsp.setErrCode(SRSP.ERRCODE_ERROR); srsp.setErrCode("上传失败,读取到的手机号码列表为空"); return srsp; } File path=new File(FileUpload); File file=new File(FileUpload+"/"+ UUIDUtil.getUuid()+".txt"); if (!path.exists()){ path.mkdirs(); } //创建一个txt if (!file.exists()){ try { boolean fileFlag=file.createNewFile(); logger.info("txt文件是否创建成功:{}",fileFlag); } catch (IOException e) { e.printStackTrace(); logger.info("创建一个txt文件失败:{}",e); } } //拼接手机号码 StringBuffer write = new StringBuffer(); for (String registerId : registeridList) { write.append(registerId+"\n"); } //往txt里写入内容 FileOutputStream outputStream=null; try { outputStream=new FileOutputStream(file); outputStream.write(write.toString().getBytes("UTF-8")); //上传txt SRSP txtSrsp=toolboxService.uploadFile(readStream(file),"txt"); logger.info("上传txt返回:{}", JSONObject.toJSONString(txtSrsp)); if (txtSrsp==null || !txtSrsp.getErrCode().equals(SRSP.ERRCODE_SUCCESSFUL)){ return txtSrsp; } srsp.setFriendErrMsg("上传成功"); srsp.setErrCode(SRSP.ERRCODE_SUCCESSFUL); srsp.setErrMsg("上传成功"); srsp.setData(txtSrsp.getData()); return srsp; } catch (IOException e) { e.printStackTrace(); logger.info("准备写入txt内容时异常:{}",e); }finally { //删除txt if (outputStream!=null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } logger.info("冗余excel文件是否删除成功:{}",file.delete()); } srsp.setFriendErrMsg("上传失败"); srsp.setErrCode(SRSP.ERRCODE_ERROR); srsp.setErrCode("上传失败"); return srsp; } /** * 读取手机号码 * @param fileType * @return */ public static List<String> read(InputStream is,String fileType){ Workbook wb=null; List<String> list=new ArrayList<>(); if ("xls".equals(fileType)) {//2003版本 try { wb=new HSSFWorkbook(is); return readString(wb); } catch (IOException e) { e.printStackTrace(); logger.info("2003版本Excel读取失败,路径:"+e.getMessage() ); return list; } }else {//2007版本 try { wb=new XSSFWorkbook(is); return readString(wb); } catch (IOException e) { e.printStackTrace(); logger.info("2007版本Excel读取失败:"+e.getMessage() ); return list; } } } public static List<String> readString(Workbook wb){ Sheet sheet=wb.getSheetAt(0);//第一个工作簿 int Hrows=sheet.getPhysicalNumberOfRows();//行数 int Lrows=0;//列 if (Hrows>1 && sheet.getRow(1)!=null) { Lrows=sheet.getRow(1).getPhysicalNumberOfCells();//去掉第一列 头 } List<String> list=new ArrayList<>(); String mobilePhone=null; for (int h = 1; h < Hrows; h++) { Row row=sheet.getRow(h); if (row==null) { logger.info("第("+h+1+"行数据为null!)"); continue; } for (int l = 0; l <Lrows ; l++) { Cell cell=row.getCell(l); if (cell!=null) {//目前只获取一个 cell.setCellType(Cell.CELL_TYPE_STRING); if (l==0) { //第二行的第一列 目前只有一列 mobilePhone=cell.getStringCellValue(); list.add(mobilePhone); mobilePhone=null; } }else{ logger.info("第("+l+1+"列数据为null!)"); } } } logger.info("文件解析完成:行数="+Lrows+",list.size()="+list.size()+",文件解析是否完整:"+(Lrows==list.size())); return list; } @Override public void close() throws Exception { logger.info("按创建顺序关闭。。。"); } /** * 根据文件路径解析成byte[] * @param file * @return * @throws Exception */ public static byte[] readStream(File file){ ByteArrayOutputStream bos=null; BufferedInputStream in=null; try{ if(!file.exists()){ throw new FileNotFoundException("file not exists"); } bos=new ByteArrayOutputStream((int)file.length()); in=new BufferedInputStream(new FileInputStream(file)); int buf_size=1024; byte[] buffer=new byte[buf_size]; int len=0; while(-1 != (len=in.read(buffer,0,buf_size))){ bos.write(buffer,0,len); } return bos.toByteArray(); } catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); return null; } finally{ try{ if(in!=null){ in.close(); } if(bos!=null){ bos.close(); } } catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); } } } /** * 删除指定目录下的文件、文件夹 * * @param dir * @return */ private boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); //递归删除目录中的子目录下 for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir + "/" + children[i])); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); }