List of usage examples for org.apache.poi.util IOUtils closeQuietly
public static void closeQuietly(final Closeable closeable)
From source file:at.tugraz.sss.serv.SSPDFFromXHTMLImageEmbedder.java
License:Apache License
@Override public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox, UserAgentCallback uac, int cssWidth, int cssHeight) { Element element = blockBox.getElement(); if (element == null) { return null; }/*from www. j ava2 s .c om*/ String nodeName = element.getNodeName(); String className = element.getAttribute("class"); String src = element.getAttribute("src"); if (!"img".equals(nodeName) || !className.contains("xmyImagex") || src == null) { return superFactory.createReplacedElement(layoutContext, blockBox, uac, cssWidth, cssHeight); } InputStream in = null; try { try { in = new FileInputStream(src); } catch (Exception error) { throw error; } final byte[] bytes = IOUtils.toByteArray(in); final Image image = Image.getInstance(bytes); final FSImage fsImage = new ITextFSImage(image); fsImage.scale(cssWidth, cssHeight); return new ITextImageElement(fsImage); } catch (Exception error) { return superFactory.createReplacedElement(layoutContext, blockBox, uac, cssWidth, cssHeight); } finally { IOUtils.closeQuietly(in); } }
From source file:at.tugraz.sss.serv.util.SSPDFFromXHTMLImageEmbedder.java
License:Apache License
@Override public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox, UserAgentCallback uac, int cssWidth, int cssHeight) { Element element = blockBox.getElement(); if (element == null) { return null; }// w w w. j ava 2 s .c o m String nodeName = element.getNodeName(); String className = element.getAttribute("class"); String src = element.getAttribute("src"); if (!"img".equals(nodeName) || !className.contains("xmyImagex") || src == null) { return superFactory.createReplacedElement(layoutContext, blockBox, uac, cssWidth, cssHeight); } InputStream in = null; try { try { in = new FileInputStream(src); } catch (Exception error) { throw error; } final byte[] bytes = IOUtils.toByteArray(in); final Image image = Image.getInstance(bytes); final FSImage fsImage = new ITextFSImage(image); fsImage.scale(cssWidth, cssHeight); return new ITextImageElement(fsImage); } catch (Exception error) { SSLogU.debug(error); return superFactory.createReplacedElement(layoutContext, blockBox, uac, cssWidth, cssHeight); } finally { IOUtils.closeQuietly(in); } }
From source file:cn.afterturn.easypoi.cache.ImageCache.java
License:Apache License
public static byte[] getImage(String imagePath) { InputStream is = POICacheManager.getFile(imagePath); ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); try {// w w w . ja v a 2 s.c o m BufferedImage bufferImg = ImageIO.read(is); ImageIO.write(bufferImg, imagePath.substring(imagePath.lastIndexOf(".") + 1, imagePath.length()), byteArrayOut); return byteArrayOut.toByteArray(); } catch (Exception e) { LOGGER.error(e.getMessage(), e); return null; } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(byteArrayOut); } }
From source file:cn.afterturn.easypoi.cache.manager.FileLoaderImpl.java
License:Apache License
@Override public byte[] getFile(String url) { InputStream fileis = null;/*from w w w .ja v a 2 s. c o m*/ ByteArrayOutputStream baos = null; try { //?? if (url.startsWith("http")) { URL urlObj = new URL(url); URLConnection urlConnection = urlObj.openConnection(); urlConnection.setConnectTimeout(30 * 1000); urlConnection.setReadTimeout(60 * 1000); urlConnection.setDoInput(true); fileis = urlConnection.getInputStream(); } else { //?,? try { fileis = new FileInputStream(url); } catch (FileNotFoundException e) { //? fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); } } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = fileis.read(buffer)) > -1) { baos.write(buffer, 0, len); } baos.flush(); return baos.toByteArray(); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(fileis); IOUtils.closeQuietly(baos); } LOGGER.error(fileis + ","); return null; }
From source file:cn.afterturn.easypoi.excel.ExcelImportUtil.java
License:Apache License
/** * Excel ??,? Integer,Long,Double,Date,String,Boolean * // ww w . ja v a 2 s . c om * @param file * @param pojoClass * @param params * @return */ public static <T> List<T> importExcel(File file, Class<?> pojoClass, ImportParams params) { FileInputStream in = null; try { in = new FileInputStream(file); return new ExcelImportService().importExcelByIs(in, pojoClass, params, false).getList(); } catch (ExcelImportException e) { throw new ExcelImportException(e.getType(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(e.getMessage(), e); } finally { IOUtils.closeQuietly(in); } }
From source file:cn.afterturn.easypoi.excel.ExcelImportUtil.java
License:Apache License
/** * Excel ?? Integer,Long,Double,Date,String,Boolean * ?,?Key-Value/*ww w. ja va 2s.c o m*/ * @param file * @param pojoClass * @param params * @return */ public static <T> ExcelImportResult<T> importExcelMore(File file, Class<?> pojoClass, ImportParams params) { FileInputStream in = null; try { in = new FileInputStream(file); return new ExcelImportService().importExcelByIs(in, pojoClass, params, true); } catch (ExcelImportException e) { throw new ExcelImportException(e.getType(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(e.getMessage(), e); } finally { IOUtils.closeQuietly(in); } }
From source file:cn.afterturn.easypoi.excel.imports.base.ImportBaseService.java
License:Apache License
public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, Workbook book) throws Exception { String path = getSaveExcelUrl(params, pojoClass); File savefile = new File(path); if (!savefile.exists()) { savefile.mkdirs();//from ww w .jav a 2 s .c o m } SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); FileOutputStream fos = new FileOutputStream(path + File.separator + format.format(new Date()) + "_" + Math.round(Math.random() * 100000) + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); book.write(fos); IOUtils.closeQuietly(fos); }
From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java
License:Apache License
/** * Excel field Integer,Long,Double,Date,String,Boolean *//* w w w . java 2 s . com*/ public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, ImportParams params, boolean needMore) throws Exception { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Excel import start ,class is {}", pojoClass); } List<T> result = new ArrayList<T>(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ExcelImportResult importResult; try { byte[] buffer = new byte[1024]; int len; while ((len = inputstream.read(buffer)) > -1) { baos.write(buffer, 0, len); } baos.flush(); InputStream userIs = new ByteArrayInputStream(baos.toByteArray()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Excel clone success"); } Workbook book = WorkbookFactory.create(userIs); boolean isXSSFWorkbook = !(book instanceof HSSFWorkbook); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Workbook create success"); } importResult = new ExcelImportResult(); createErrorCellStyle(book); Map<String, PictureData> pictures; for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() + params.getSheetNum(); i++) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(" start to read excel by is ,startTime is {}", new Date()); } if (isXSSFWorkbook) { pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book); } else { pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book); } if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel by is ,endTime is {}", new Date()); } result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel list by sheet ,endTime is {}", new Date()); } if (params.isReadSingleCell()) { readSingleCell(importResult, book.getSheetAt(i), params); if (LOGGER.isDebugEnabled()) { LOGGER.debug(" read Key-Value ,endTime is {}", System.currentTimeMillis()); } } } if (params.isNeedSave()) { saveThisExcel(params, pojoClass, isXSSFWorkbook, book); } importResult.setList(result); if (needMore) { InputStream successIs = new ByteArrayInputStream(baos.toByteArray()); try { Workbook successBook = WorkbookFactory.create(successIs); importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params)); importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params)); importResult.setFailList(failCollection); importResult.setVerfiyFail(verifyFail); } finally { successIs.close(); } } } finally { IOUtils.closeQuietly(baos); } return importResult; }
From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java
License:Apache License
/** * @param object/*from w w w.ja v a2s. co m*/ * @param picId * @param excelParams * @param titleString * @param pictures * @param params * @throws Exception */ private void saveImage(Object object, String picId, Map<String, ExcelImportEntity> excelParams, String titleString, Map<String, PictureData> pictures, ImportParams params) throws Exception { if (pictures == null) { return; } PictureData image = pictures.get(picId); if (image == null) { return; } byte[] data = image.getData(); String fileName = "pic" + Math.round(Math.random() * 100000000000L); fileName += "." + PoiPublicUtil.getFileExtendName(data); if (excelParams.get(titleString).getSaveType() == 1) { String path = getSaveUrl(excelParams.get(titleString), object); File savefile = new File(path); if (!savefile.exists()) { savefile.mkdirs(); } savefile = new File(path + File.separator + fileName); FileOutputStream fos = new FileOutputStream(savefile); try { fos.write(data); } finally { IOUtils.closeQuietly(fos); } setValues(excelParams.get(titleString), object, getSaveUrl(excelParams.get(titleString), object) + File.separator + fileName); } else { setValues(excelParams.get(titleString), object, data); } }
From source file:cn.bzvs.excel.imports.base.ImportBaseService.java
License:Apache License
public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, Workbook book) throws Exception { String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass)); File savefile = new File(path); if (!savefile.exists()) { savefile.mkdirs();//from ww w .ja va 2 s . c o m } SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); FileOutputStream fos = new FileOutputStream(path + "/" + format.format(new Date()) + "_" + Math.round(Math.random() * 100000) + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); book.write(fos); IOUtils.closeQuietly(fos); }