本文整理匯總了Java中org.apache.poi.util.IOUtils類的典型用法代碼示例。如果您正苦於以下問題:Java IOUtils類的具體用法?Java IOUtils怎麽用?Java IOUtils使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
IOUtils類屬於org.apache.poi.util包,在下文中一共展示了IOUtils類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: process
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
/**
* could be interesting for spring batch
*
* @param inputResource source of the data read by lines
* @param outputResource generated .xlsx resource
*/
public void process(Resource inputResource, Resource outputResource) {
SXSSFWorkbook wb = new SXSSFWorkbook(flushSize);
try {
wb.setCompressTempFiles(true); // temp files will be gzipped
final Sheet sh = wb.createSheet();
InputStreamReader is = new InputStreamReader(inputResource.getInputStream());
BufferedReader br = new BufferedReader(is);
final CreationHelper createHelper = sh.getWorkbook().getCreationHelper();
br.lines().forEachOrdered(string -> createRow(string, sh, createHelper));
FileOutputStream out = new FileOutputStream(outputResource.getFile());
wb.write(out);
IOUtils.closeQuietly(out);
} catch (IOException e) {
logger.error("IOE", e);
} finally {
if (wb != null) {
// dispose of temporary files backing this workbook on disk
wb.dispose();
}
}
}
示例2: filesZip
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
@RequestMapping(value = "/files.zip")
@ResponseBody
byte[] filesZip() throws IOException {
File dir = new File("./");
File[] filesArray = dir.listFiles();
if (filesArray == null || filesArray.length == 0)
System.out.println(dir.getAbsolutePath() + " have no file!");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ZipOutputStream zipOut= new ZipOutputStream(bo);
for(File xlsFile:filesArray){
if(!xlsFile.isFile())continue;
ZipEntry zipEntry = new ZipEntry(xlsFile.getName());
zipOut.putNextEntry(zipEntry);
zipOut.write(IOUtils.toByteArray(new FileInputStream(xlsFile)));
zipOut.closeEntry();
}
zipOut.close();
return bo.toByteArray();
}
示例3: addAndPersistProduct
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
/**
* ## todo : add proper mapping.
*
* @param request {@link HttpServletRequest}
*/
@PostMapping("add/product")
public Map<String, Boolean> addAndPersistProduct(final HttpServletRequest request) {
final MultipartFile product = ((StandardMultipartHttpServletRequest) request).getMultiFileMap().getFirst("products");
final MultipartFile image = ((StandardMultipartHttpServletRequest) request).getMultiFileMap().getFirst("images");
// manually replace quote char
final String commaSeparatedKeywords = request.getParameter("keywords").replaceAll("\"", "");
final String userName = request.getParameter("username");
final UserEntity userEntity = this.service.getUserByUserName(userName);
final List<KeywordEntity> keywordEntityList = this.service.getKeywordEntityListByAlias(commaSeparatedKeywords);
final ProductEntity productEntity = new ProductEntity();
// manually replace quote char
productEntity.setName(request.getParameter("name").replaceAll("\"", ""));
productEntity.setDescription(request.getParameter("description").replaceAll("\"", ""));
try (final InputStream imgInput = image.getInputStream();
final InputStream cntInput = product.getInputStream()) {
final byte[] imgArray = IOUtils.toByteArray(imgInput); // Apache commons IO.
productEntity.setPreviewImage(imgArray);
final byte[] cntArray = IOUtils.toByteArray(cntInput); // Apache commons IO.
productEntity.setProductItem(cntArray);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
productEntity.setUserEntity(userEntity);
productEntity.setKeywordEntityList(keywordEntityList);
this.service.persistProductEntity(productEntity);
final Map<String, Boolean> returnMap = new HashMap<>();
returnMap.put("success", productEntity.getProductUuid() != null);
return returnMap;
}
示例4: download
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
public static void download(String from, String to) {
InputStream in = null;
OutputStream out = null;
try {
URL apdf = new URL(from);
in = apdf.openStream();
System.out.println("開始下載:" + from);
out = new FileOutputStream(new File(to));
IOUtils.copy(in, out);
out.flush();
System.out.println("下載完成:" + to);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
示例5: create
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
/**
* @param inp
* @param excelReader
* @param ignoreNumFormat 是否忽略數據格式 (default=false,按照格式讀取)
* @return jie
* @throws InvalidFormatException
* @throws IOException
* @throws SQLException
*/
public static ReadHandler create(InputStream inp,
ExcelReadListener excelReader, boolean ignoreNumFormat)
throws InvalidFormatException, IOException, SQLException {
// If clearly doesn't do mark/reset, wrap up
if (! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
// Ensure that there is at least some data there
byte[] header8 = IOUtils.peekFirst8Bytes(inp);
// Try to create
if (POIFSFileSystem.hasPOIFSHeader(header8)) {
POIFSFileSystem fs = new POIFSFileSystem(inp);
return create(fs, excelReader, ignoreNumFormat);
}
if (POIXMLDocument.hasOOXMLHeader(inp)) {
OPCPackage pkg = OPCPackage.open(inp);
return create(pkg, excelReader, ignoreNumFormat);
}
throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
示例6: getTemplate
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
@RequestMapping(value = "/emp/myview/downloadTemplate.do", method = RequestMethod.GET)
public void getTemplate(HttpServletRequest request,
HttpServletResponse response) {
ClassLoader classLoader = this.getClass().getClassLoader();
URL url = null;
File file = null;
FileInputStream fStream = null;
try {
url = classLoader.getResource("ProjectDescription.xlsx");
file = new File(url.getPath());
fStream = new FileInputStream(file);
String headerValue = String.format("attachment; filename=\"%s\"",
file.getName());
response.setHeader("Content-Disposition", headerValue);
response.setContentLength((int) file.length());
response.setContentType(context.getMimeType(url.getPath()));
org.apache.commons.io.IOUtils.copy(fStream,
response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: getItemDescriptionTemplate
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
@RequestMapping(value = "/emp/myview/downloadItemDescTemplate.do", method = RequestMethod.GET)
public void getItemDescriptionTemplate(HttpServletRequest request,
HttpServletResponse response) {
ClassLoader classLoader = this.getClass().getClassLoader();
URL url = null;
File file = null;
FileInputStream fStream = null;
try {
url = classLoader.getResource("ItemDescription.xlsx");
file = new File(url.getPath());
fStream = new FileInputStream(file);
String headerValue = String.format("attachment; filename=\"%s\"",
file.getName());
response.setHeader("Content-Disposition", headerValue);
response.setContentLength((int) file.length());
response.setContentType(context.getMimeType(url.getPath()));
org.apache.commons.io.IOUtils.copy(fStream,
response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: isNewExcelFormat
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
/**
* Detect the excel format with only peeking at the first 8 bytes of the input stream (leaving the stream untouched).
*
* @param inputStream the xls input stream.
* @return true if the given input stream is a xls new format.
* @throws IOException if an error occurs.
*/
public static boolean isNewExcelFormat(InputStream inputStream) throws IOException {
boolean newFormat = false;
// Ensure that there is at least some data there
byte[] headers = IOUtils.peekFirst8Bytes(inputStream);
if (NPOIFSFileSystem.hasPOIFSHeader(headers)) {
newFormat = false;
}
if (POIXMLDocument.hasOOXMLHeader(new ByteArrayInputStream(headers))) {
newFormat = true;
}
return newFormat;
}
示例9: decompress
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
/**
* Decompresses the whole of the compressed RTF
* stream, outputting the resulting RTF bytes.
* Note - will decompress any padding at the end of
* the input, if present, use {@link #getDeCompressedSize()}
* if you need to know how much of the result is
* real. (Padding may be up to 7 bytes).
*/
public void decompress(InputStream src, OutputStream res) throws IOException {
// Validate the header on the front of the RTF
compressedSize = LittleEndian.readInt(src);
decompressedSize = LittleEndian.readInt(src);
int compressionType = LittleEndian.readInt(src);
int dataCRC = LittleEndian.readInt(src);
// TODO - Handle CRC checking on the output side
// Do we need to do anything?
if(compressionType == UNCOMPRESSED_SIGNATURE_INT) {
// Nope, nothing fancy to do
IOUtils.copy(src, res);
} else if(compressionType == COMPRESSED_SIGNATURE_INT) {
// We need to decompress it below
} else {
throw new IllegalArgumentException("Invalid compression signature " + compressionType);
}
// Have it processed
super.decompress(src, res);
}
示例10: generateLogo
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
public void generateLogo(Sheet sheet, int length, Workbook wb, String basePath, Image image) {
if (image == null) {
return;
}
try {
sheet.addMergedRegion(new CellRangeAddress(image.getRowFrom(), (short) image.getRowTo() - 1,
image.getColFrom(), (short) image.getColTo() - 1));
String imageFilePath = "d:/app/img";
InputStream is = new FileInputStream(basePath + imageFilePath);
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,
image.getColFrom(), image.getRowFrom(), image.getColTo(), image.getRowTo() + 1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
} catch (IOException e) {
e.printStackTrace();
}
}
示例11: copyToTempFile
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
private File copyToTempFile(InputStream is) throws IOException {
String partName = packagePart.getPartName().getName();
partName = partName.substring(partName.lastIndexOf('/')+1);
final int idx = partName.lastIndexOf('.');
final String prefix = ((idx == -1) ? partName : partName.substring(0, idx)) + "-";
final String suffix = (idx == -1 || idx == partName.length()-1) ? "" : partName.substring(idx);
final File of = TempFile.createTempFile(prefix, suffix);
try (FileOutputStream fos = new FileOutputStream(of)) {
IOUtils.copy(is, fos);
}
of.deleteOnExit();
return of;
}
示例12: copyToTempFile
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
private File copyToTempFile(InputStream is) throws IOException {
final String partName = entry.getName();
final int idx = partName.lastIndexOf('.');
final String prefix = ((idx == -1) ? partName : partName.substring(0, idx)) + "-";
final String suffix = (idx == -1 || idx == partName.length()-1) ? "" : partName.substring(idx);
final File of = TempFile.createTempFile(prefix, suffix);
try (FileOutputStream fos = new FileOutputStream(of)) {
IOUtils.copy(is, fos);
}
of.deleteOnExit();
return of;
}
示例13: copyToTempFile
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
private File copyToTempFile(InputStream is) throws IOException {
final String prefix = "embed-"+embed.getPersistId()+"-";
final String suffix = ".dat";
final File of = TempFile.createTempFile(prefix, suffix);
try (FileOutputStream fos = new FileOutputStream(of)) {
IOUtils.copy(is, fos);
}
of.deleteOnExit();
return of;
}
示例14: copyToTempFile
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
private File copyToTempFile(InputStream is) throws IOException {
final String prefix = record.getClass().getSimpleName() + "-";
final String suffix = ".zip";
final File of = TempFile.createTempFile(prefix, suffix);
try (FileOutputStream fos = new FileOutputStream(of)) {
IOUtils.copy(is, fos);
}
of.deleteOnExit();
return of;
}
示例15: clearCurrentFile
import org.apache.poi.util.IOUtils; //導入依賴的package包/類
public void clearCurrentFile() {
Object userObject = treeRoot.getUserObject();
if (userObject instanceof TreeModelEntry) {
IOUtils.closeQuietly((TreeModelEntry)userObject);
treeRoot.setUserObject(null);
}
treeRoot.removeAllChildren();
treeRoot.setUserObject("Not loaded ...");
treeModel.reload(treeRoot);
}