当前位置: 首页>>代码示例>>Java>>正文


Java IOUtils.closeQuietly方法代码示例

本文整理汇总了Java中org.apache.tika.io.IOUtils.closeQuietly方法的典型用法代码示例。如果您正苦于以下问题:Java IOUtils.closeQuietly方法的具体用法?Java IOUtils.closeQuietly怎么用?Java IOUtils.closeQuietly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.tika.io.IOUtils的用法示例。


在下文中一共展示了IOUtils.closeQuietly方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: analyze

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
@Override
public Iterator<byte[]> analyze(CrawleableUri curi, File data, Sink sink) {
    FileInputStream fin = null;
    try {
        // First, try to get the language of the data
        Lang lang = null;
        String contentType = (String) curi.getData(HttpHeaders.CONTENT_TYPE);
        if (contentType != null) {
            lang = RDFLanguages.contentTypeToLang(contentType);
        } else {
            lang = RDFLanguages.filenameToLang(data.getName(), null);
        }
        FilterSinkRDF filtered = new FilterSinkRDF(curi, sink);
        RDFDataMgr.parse(filtered, data.getAbsolutePath(), lang);
    } catch (Exception e) {
        LOGGER.error("Exception while analyzing. Aborting.");
    } finally {
        IOUtils.closeQuietly(fin);
    }
    return collector.getUris(curi);
}
 
开发者ID:dice-group,项目名称:Squirrel,代码行数:22,代码来源:RDFAnalyzer.java

示例2: detectArchiveFormat

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private static MediaType detectArchiveFormat(byte[] prefix, int length) {
    try {
        ArchiveStreamFactory factory = new ArchiveStreamFactory();
        ArchiveInputStream ais = factory.createArchiveInputStream(
                new ByteArrayInputStream(prefix, 0, length));
        try {
            if ((ais instanceof TarArchiveInputStream)
                    && !TarArchiveInputStream.matches(prefix, length)) {
                // ArchiveStreamFactory is too relaxed, see COMPRESS-117
                return MediaType.OCTET_STREAM;
            } else {
                return PackageParser.getMediaType(ais);
            }
        } finally {
            IOUtils.closeQuietly(ais);
        }
    } catch (ArchiveException e) {
        return MediaType.OCTET_STREAM;
    }
}
 
开发者ID:kolbasa,项目名称:OCRaptor,代码行数:21,代码来源:ZipContainerDetector.java

示例3: extractMetadata

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private StreamParserThread extractMetadata(final InputStream stream, final Metadata metadata) {
    StreamParserThread parserThread = new StreamParserThread() {
        public void run() {
           BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
           try {
              String line;
              while ( (line = reader.readLine()) != null ) {
                 for(Pattern p : metadataPatterns.keySet()) {
                    Matcher m = p.matcher(line);
                    if(m.find()) {
                       metadata.add( metadataPatterns.get(p), m.group(1) );
                    }
                 }
              }
              this.isComplete = true;
           } catch (IOException e) {
               this.ioException = e;
           } finally {
              IOUtils.closeQuietly(reader);
              IOUtils.closeQuietly(stream);
          }
        }
    };
    parserThread.start();
    return parserThread;
}
 
开发者ID:AlfrescoLabs,项目名称:tika-ffmpeg,代码行数:27,代码来源:WaitingExternalParser.java

示例4: closeSinkForUri

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
@Override
public void closeSinkForUri(CrawleableUri uri) {
    String uriString = uri.getUri().toString();
    if (streamMapping.containsKey(uriString)) {
        IOUtils.closeQuietly(streamMapping.get(uriString));
        streamMapping.remove(uriString);
    } else {
        LOGGER.error("Should close the sink for the URI \"" + uriString + "\" but couldn't find it.");
    }
}
 
开发者ID:dice-group,项目名称:Squirrel,代码行数:11,代码来源:FileBasedSink.java

示例5: execute

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private void execute(Path indexFile, Path rootDir, Path statusFile) throws IOException {

        int count = 0;
        BufferedWriter writer = Files.newBufferedWriter(statusFile, StandardCharsets.UTF_8);
        InputStream is = null;
        try {
            if (indexFile.endsWith(".gz")) {
                is = new BufferedInputStream(new GZIPInputStream(Files.newInputStream(indexFile)));
            } else {
                is = new BufferedInputStream(Files.newInputStream(indexFile));
            }
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
                String line = reader.readLine();
                while (line != null) {
                    processRow(line, rootDir, writer);
                    if (++count % 100 == 0) {
                        logger.info(indexFile.getFileName().toString() + ": " + count);
                    }
                    line = reader.readLine();
                }

            }
        } finally {
            IOUtils.closeQuietly(is);
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
开发者ID:tballison,项目名称:SimpleCommonCrawlExtractor,代码行数:33,代码来源:CCGetter.java

示例6: detectCompressorFormat

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private static MediaType detectCompressorFormat(byte[] prefix, int length) {
    try {
        CompressorStreamFactory factory = new CompressorStreamFactory();
        CompressorInputStream cis = factory.createCompressorInputStream(
                new ByteArrayInputStream(prefix, 0, length));
        try {
            return CompressorParser.getMediaType(cis);
        } finally {
            IOUtils.closeQuietly(cis);
        }
    } catch (CompressorException e) {
        return MediaType.OCTET_STREAM;
    }
}
 
开发者ID:kolbasa,项目名称:OCRaptor,代码行数:15,代码来源:ZipContainerDetector.java

示例7: fromCsv

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
public static List<String[]> fromCsv(String rows) {
  Reader stringReader = new StringReader(rows);
  try {
    return new CSVReader(stringReader).readAll();
  } catch (Throwable t) {
    Log.exception(t);
    return null;
  } finally {
    IOUtils.closeQuietly(stringReader);
  }
}
 
开发者ID:MachinePublishers,项目名称:ScreenSlicer,代码行数:12,代码来源:Spreadsheet.java

示例8: writeToTempFile

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private InputStream writeToTempFile() throws IOException {
  final File f = File.createTempFile("spreadsheet", "poi");
  FileOutputStream out = new FileOutputStream(f);
  workbook.write(out);
  IOUtils.closeQuietly(out);
  return new FileInputStream(f) {
    @Override
    public void close() throws IOException {
      super.close();
      FileUtils.deleteQuietly(f);
    }
  };
}
 
开发者ID:uq-eresearch,项目名称:aorra,代码行数:14,代码来源:SpreadsheetDataSource.java

示例9: download

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private boolean download(String src, String local, File destination, String playSession) {
    try {
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        URI u = new URI(src);
        String url;
        if(u.isAbsolute()) {
            url = u.toString();
        } else {
            url = "http://localhost:9000" + u.toString();
            HttpState state = new HttpState();
            Cookie session = new Cookie("localhost", "PLAY_SESSION",
                    playSession, "/", -1, false);
            state.addCookie(session);
            client.setState(state);
        }
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(true);
        client.executeMethod(method);
        if(method.getStatusCode() == 200) {
            InputStream in = method.getResponseBodyAsStream();
            File f = new File(destination, local);
            f.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(new File(destination, local));
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            method.releaseConnection();
            return true;
        } else {
            method.releaseConnection();
            return false;
        }
    } catch(Exception e) {
        return false;
    }
}
 
开发者ID:uq-eresearch,项目名称:aorra,代码行数:37,代码来源:HtmlZip.java

示例10: writeConstellioDefaultSchema

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private static void writeConstellioDefaultSchema(Document schemaDocument) {
	File schemaFile = new File(ClasspathUtils.getCollectionsRootDir() + File.separator + SolrCoreContext.DEFAULT_COLLECTION_NAME + File.separator + "conf" + File.separator + "schema.xml");
	FileOutputStream fos = null;
	try {
		OutputFormat format = OutputFormat.createPrettyPrint();
		fos = new FileOutputStream(schemaFile);
		XMLWriter writer = new XMLWriter(fos, format);
		writer.write(schemaDocument);
		writer.close();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		IOUtils.closeQuietly(fos);
	}
}
 
开发者ID:BassJel,项目名称:Jouve-Project,代码行数:16,代码来源:SolrServicesImpl.java

示例11: requestData

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
protected File requestData(CrawleableUri uri, File outputFile)
        throws ClientProtocolException, FileNotFoundException, IOException {
    HttpGet request = null;
    request = new HttpGet(uri.getUri());
    request.addHeader(HttpHeaders.ACCEPT,
            MapUtils.getString(uri.getData(), Constants.URI_HTTP_ACCEPT_HEADER, acceptHeader));
    request.addHeader(HttpHeaders.ACCEPT_CHARSET,
            MapUtils.getString(uri.getData(), Constants.URI_HTTP_ACCEPT_HEADER, acceptCharset));

    HttpEntity entity = null;
    CloseableHttpResponse response = null;
    OutputStream os = null;
    try {
        response = client.execute(request);

        // Handle response headers (especially the status and the content type)
        for (Header header : response.getAllHeaders()) {
            uri.addData(HTTP_RESPONSE_HEADER_PREFIX + header.getName(), header.getValue());
        }
        StatusLine status = response.getStatusLine();
        uri.addData(Constants.URI_HTTP_STATUS_CODE, status.getStatusCode());
        if ((status.getStatusCode() < 200) || (status.getStatusCode() >= 300)) {
            LOGGER.info("Response of \"{}\" has the wrong status ({}). Returning null.", uri, status.toString());
            return null;
        }
        Header contentTypeHeader = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
        if (contentTypeHeader != null) {
            String typeValues[] = contentTypeHeader.getValue().split(";");
            uri.addData(Constants.URI_HTTP_MIME_TYPE_KEY, typeValues[0]);
            // If the content type contains a charset
            if (typeValues.length > 0) {
                uri.addData(Constants.URI_HTTP_CHARSET_KEY, typeValues[1]);
            }
        } else {
            LOGGER.info("The response did not contain a content type header.");
        }

        // store response data
        entity = response.getEntity();
        InputStream is = entity.getContent();
        os = new BufferedOutputStream(new FileOutputStream(outputFile));
        StreamUtils.copy(is, os);
    } finally {
        IOUtils.closeQuietly(os);
        if (entity != null) {
            try {
                EntityUtils.consume(entity);
            } catch (IOException e1) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    }
    uri.addData(Constants.URI_DATA_FILE_NAME, outputFile.getAbsolutePath());
    return outputFile;
}
 
开发者ID:dice-group,项目名称:Squirrel,代码行数:61,代码来源:HTTPFetcher.java

示例12: checkModel

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
private void checkModel(Model model, URI uri, boolean useCompression) {
    String fileName = FileBasedSink.generateFileName(uri.toString(), useCompression);
    File file = new File(tempDirectory.getAbsolutePath() + File.separator + fileName);
    if (model.size() == 0) {
        Assert.assertFalse("found a file " + file.getAbsolutePath() + " while the model of " + uri.toString()
                + " was empty (and shouldn't create a file).", file.exists());
        return;
    } else {
        Assert.assertTrue("Couldn't find the file " + file.getAbsolutePath() + " for model " + uri.toString(),
                file.exists());
    }

    Model readModel = null;
    Dataset readData = DatasetFactory.createMem();
    InputStream in = null;

    try {
        in = new FileInputStream(file);
        if (useCompression) {
            in = new GZIPInputStream(in);
        }
        RDFDataMgr.read(readData, in, Lang.NQ);
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail("Couldn't read file for model " + uri.toString());
    } finally {
        IOUtils.closeQuietly(in);
    }

    readModel = readData.getNamedModel(uri.toString());
    String errorMsg = "The read model of " + uri.toString() + ": " + readModel
            + " does not fit the expected model: " + model;
    StmtIterator iterator = model.listStatements();
    Statement s;
    while (iterator.hasNext()) {
        s = iterator.next();
        Assert.assertTrue(errorMsg + " The read Model does not contain " + s, readModel.contains(s));
    }
    iterator = readModel.listStatements();
    while (iterator.hasNext()) {
        s = iterator.next();
        Assert.assertTrue(errorMsg + " The read Model has the additional triple " + s, readModel.contains(s));
    }
}
 
开发者ID:dice-group,项目名称:Squirrel,代码行数:45,代码来源:FileBasedSinkTest.java

示例13: upload

import org.apache.tika.io.IOUtils; //导入方法依赖的package包/类
/**
 * 如果上传图片需要传入PIC_TYPE参数则请参考file.xml
 *
 * @param responseUrl 为跨域上传图片的解决方案
 * @param transaction 上传多个文件时,是否事务控制。即当一个文件上传失败,所有的文件都上传失败 <br>
 * 当没用事务时,上传文件失败的错误信息会存放在fileInfo中error属性中,否则错误信息存放于CodeMsg的msg属性。<br>
 * transaction 默认值: false
 * @param fileType 文件类型
 * @param fileKey 文件标识,参考file.xml map中entry标签的key属性
 */
@RequestMapping("/upload")
public void upload(String responseUrl, boolean transaction, FileType fileType, String fileKey,
                   @RequestPart("files") MultipartFile[] files, HttpServletResponse resp) throws Exception {
	CodeMsg error = null;
	String filePath = null; //指定文件类型路径
	
	if (fileType == null) {
		error = CodeMsgExt.PARAM_BLANK.fillArgs("fileType");
	} else if (fileKey == null) {
		error = CodeMsgExt.PARAM_BLANK.fillArgs("fileKey");
	} else if (files.length == 0) {
		error = CodeMsgExt.NO_FILE;
	} else {
		filePath = fileType.getUploadPath(fileKey);
		if (filePath == null) {
			error = CodeMsgExt.PARAM_ILLEGAL.fillArgs("fileKey", fileKey);
		}
	}
	if (error != null) {
		outData(error, responseUrl, resp);
		return;
	}
	
	InputStream[] streams = null;
	try {
		streams = new InputStream[files.length];
		long[] lengths = new long[files.length];
		for(int i = 0; i < files.length; i++) {
			streams[i] = files[i].getInputStream();
			lengths[i] = files[i].getSize();
		}
		
		if (fileType == FileType.PIC_TYPE) {
			//上传文件为图片类型
			error = ImageTools.validateImgs(filePath, streams, lengths, null, transaction);
			if (!error.isSuc()) {
				outData(error, responseUrl, resp);
				return ;
			}
			Image[] imgs = (Image[])error.getData();
			imgs = uploadHandler.uploadImg(fileType, files, filePath, imgs, null, transaction);
			
			error = CodeMsgExt.SUC.data(imgs);
		} else if(fileType == FileType.MIX_TYPE) {
			//上传文件为混合类型,可以是图片、TXT、ZIP等
			String[] filenames = new String[files.length];
			for(int i = 0; i < files.length; i++) {
				filenames[i] = FilenameUtils.getName(files[i].getOriginalFilename());
			}
			error = Files.validate(filePath, streams, filenames, lengths, null, transaction);
			if (!error.isSuc()) {
				outData(error, responseUrl, resp);
				return ;
			}
			FileInfo[] fileInfos = (FileInfo[])error.getData();
			fileInfos = uploadHandler.uploadFile(fileType, files, filePath, fileInfos, null, transaction);
			
			error = CodeMsgExt.SUC.data(fileInfos);
		}
	} catch (Exception e) {
		error = CodeMsgExt.FAIL.msg("上传失败");
		log.error("上传文件失败", e);
	} finally {
		if(streams != null) {
			for (InputStream is : streams) {
				IOUtils.closeQuietly(is);
			}
		}
	}
	outData(error, responseUrl, resp);
}
 
开发者ID:easycodebox,项目名称:easycode,代码行数:82,代码来源:UploadController.java


注:本文中的org.apache.tika.io.IOUtils.closeQuietly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。