本文整理汇总了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);
}
示例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;
}
}
示例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;
}
示例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.");
}
}
示例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();
}
}
}
示例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;
}
}
示例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);
}
}
示例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);
}
};
}
示例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;
}
}
示例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);
}
}
示例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;
}
示例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));
}
}
示例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);
}