本文整理汇总了Java中org.apache.tika.mime.MimeTypes类的典型用法代码示例。如果您正苦于以下问题:Java MimeTypes类的具体用法?Java MimeTypes怎么用?Java MimeTypes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MimeTypes类属于org.apache.tika.mime包,在下文中一共展示了MimeTypes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMimeTypeForMediaTypeSafe
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/**
* Returns the top tika mime-type definition for the media type.
* WARN: this only returns explicit defined mime-types (canonical), NOT aliases.
* if exact true, will double-check that parameters match as well (not guaranteed by MimeTypes.getRegisteredMimeType).
* FIXME: exact doesn't handle parameter order.
*/
public static MimeType getMimeTypeForMediaTypeSafe(String mediaType, MimeTypes mimeTypes, boolean exact) {
try {
MimeType mimeType = mimeTypes.getRegisteredMimeType(mediaType);
if (mimeType != null && exact) {
// NOTE: because the way getRegisteredMimeType works, it may return non-null
// even if not exact name match, due to parameters.
// FIXME: this check won't handle parameter order difference
// also check if another normalize call would be more appropriate...
if (!getMimeTypeId(mediaType).equals(getMimeTypeId(mimeType.getName()))) {
return null;
}
}
return mimeType;
} catch (MimeTypeException e) {
return null;
}
}
示例2: getMediaTypeDescriptionOrNullSafe
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public static String getMediaTypeDescriptionOrNullSafe(MediaType mediaType, MimeTypes mimeTypes) {
MimeType mimeType = getMimeTypeForMediaTypeSafe(mediaType, mimeTypes, true);
String description = null;
if (mimeType != null) {
description = mimeType.getDescription();
} else {
// when this prints, it's because of imperfections in tika-mimetypes.xml...
Debug.logWarning("No Tika mime-type for MediaType: " + mediaType.toString(), module);
}
if (UtilValidate.isNotEmpty(description)) {
return description;
} else {
MediaTypeRegistry registry = mimeTypes.getMediaTypeRegistry();
// check if can find one in supertype
MediaType superType = registry.getSupertype(mediaType);
if (superType != null) {
description = getMediaTypeDescriptionOrNullSafe(superType, mimeTypes);
if (UtilValidate.isNotEmpty(description)) {
return description + " (sub-type)";
}
}
}
return null;
}
示例3: embedding
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
@Override
public void embedding(String mimeType, byte[] data) {
File file = new File();
String embeddingName;
try {
embeddingName = MimeTypes.getDefaultMimeTypes().forName(mimeType).getType().getType();
} catch (MimeTypeException e) {
LOGGER.warn("Mime-type not found", e);
embeddingName = "embedding";
}
file.setName(embeddingName);
file.setContent(data);
Utils.sendLog(embeddingName, "UNKNOWN", file);
}
示例4: getMimeType
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public String getMimeType() throws IOException
{
TikaInputStream tikaIS = null;
try
{
tikaIS = TikaInputStream.get(file);
return new DefaultDetector(MimeTypes.getDefaultMimeTypes()).detect(tikaIS, new Metadata()).toString();
} finally
{
if (tikaIS != null)
{
tikaIS.close();
}
}
}
示例5: readLocalTextFile
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public void readLocalTextFile(String textFilePath, String customTitle) {
if (getActivity() instanceof MainActivity) {
MainActivity activity = (MainActivity) getActivity();
LocalFileImportService.ReadLocalTextFileBinder binder = activity.getReadLocalTextFileBinder();
String mime = detectMimeType(textFilePath);
if (MimeTypes.PLAIN_TEXT.equalsIgnoreCase(mime)) {
binder.addToCacheQueue(textFilePath, customTitle);
showSnackbar(getString(R.string.toast_read_local_file_background), SimpleSnackbarType.INFO);
} else {
new MaterialDialog.Builder(getActivity())
.title(R.string.dialog_error_mime_title)
.content(R.string.dialog_error_mime_content)
.positiveText(android.R.string.ok).build().show();
}
}
}
示例6: detectMimeTypes
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
@Nonnull
@Override
public List<String> detectMimeTypes(final InputStream input, @Nullable final String fileName) throws IOException {
checkNotNull(input);
List<String> detected = Lists.newArrayList();
Metadata metadata = new Metadata();
if (fileName != null) {
metadata.set(Metadata.RESOURCE_NAME_KEY, fileName);
}
MediaType mediaType;
try (final TikaInputStream tis = TikaInputStream.get(input)) {
mediaType = detector.detect(tis, metadata);
}
// unravel to least specific
unravel(detected, mediaType);
if (detected.isEmpty()) {
detected.add(MimeTypes.OCTET_STREAM);
}
return detected;
}
示例7: guessMimeTypesListFromPath
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/**
* Uses extension to mime types cache backed by {@link NexusMimeTypes} and Tika registry.
*/
@Nonnull
private List<String> guessMimeTypesListFromPath(final String path) {
checkNotNull(path);
final String pathExtension = Files.getFileExtension(path);
try {
final List<String> mimeTypes = new ArrayList<>();
List<String> extBasedTypes = extensionToMimeTypeCache.get(pathExtension);
if (extBasedTypes != null && !extBasedTypes.isEmpty()) {
mimeTypes.addAll(extBasedTypes);
}
if (mimeTypes.isEmpty()) {
mimeTypes.add(MimeTypes.OCTET_STREAM);
}
return mimeTypes;
}
catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
示例8: getExtension
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/**
* Determines extension to use for the given mime type.
* <p>Current implementation is based on <i>Apache Tika</i>. If the given mime type is not recognized no extension
* will be returned
*
* @param mimeType The mime type to get the extensio for
* @return The default extension for the given mime type<br>or <code>null</code> when the mime type is
* not recognized
*/
public static String getExtension(final String mimeType) {
if (mimeType == null || mimeType.isEmpty())
return null;
final MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
MimeType tikaMimeType = null;
try {
tikaMimeType = allTypes.forName(mimeType);
} catch (final MimeTypeException ex) {
// Can not detect the mime type of the given file, so no extension
return null;
}
return tikaMimeType.getExtension();
}
示例9: extensionForMIME
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public static String extensionForMIME(String mimeType) {
if (mimeType.isEmpty())
return DEFAULT_EXT;
MimeType mime = null;
try {
mime = MimeTypes.getDefaultMimeTypes().forName(mimeType);
} catch (MimeTypeException ex) {
LOGGER.log(Level.WARNING, "can't find mimetype", ex);
}
String m = mime != null ? mime.getExtension() : "";
// remove dot
if (!m.isEmpty())
m = m.substring(1);
return StringUtils.defaultIfEmpty(m, DEFAULT_EXT);
}
示例10: getExtension
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/**
* Get the preferred file extension for the content type
*
* @param mimeType mimeType
* @return file extension (already including '.')
*/
public static String getExtension(String mimeType) {
final String defaultExt = ".bin";
final MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
try {
final String ext = allTypes.forName(mimeType).getExtension();
log.trace("Tika's file-extension for {} is '{}'", mimeType, ext);
if (StringUtils.isNotBlank(ext)) {
return ext;
}
} catch (MimeTypeException e) {
log.trace("MimeTypeException: {}. Not critical, recovering...", e.getMessage());
}
log.trace("Using fallback file-extension '{}' for {}", defaultExt, mimeType);
return defaultExt;
}
示例11: getMimeType
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public static String getMimeType(CloseableHttpResponse response) {
try {
MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
return allTypes.forName(response.getEntity().getContentType().getValue()).getExtension();
} catch (MimeTypeException e) {
e.printStackTrace();
}
return null;
}
示例12: getMediaTypeDescriptionAlwaysSafe
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
public static String getMediaTypeDescriptionAlwaysSafe(MediaType mediaType, MimeTypes mimeTypes, String overrideDesc, String defaultDesc) {
if (UtilValidate.isNotEmpty(overrideDesc)) {
return overrideDesc;
}
String description = getMediaTypeDescriptionOrNullSafe(mediaType, mimeTypes);
if (UtilValidate.isNotEmpty(description)) {
return description;
} else {
return defaultDesc;
}
}
示例13: makeEntityMimeTypes
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/**
* WARN: very slow, not for heavy use.
*/
public static List<GenericValue> makeEntityMimeTypes(Delegator delegator, Collection<MediaType> mediaTypes, MimeTypes mimeTypes, boolean aliases, boolean missingOnly) throws GenericEntityException {
List<GenericValue> mimeTypeValues = new ArrayList<>(mediaTypes.size());
for(MediaType mediaType : mediaTypes) {
GenericValue mainValue = makeEntityMimeType(delegator, mediaType, mimeTypes, "", getMimeTypeId(mediaType));
if (!missingOnly || UtilValidate.isEmpty(delegator.findOne("MimeType", true, UtilMisc.toMap("mimeTypeId", getMimeTypeId(mediaType))))) {
mimeTypeValues.add(mainValue);
}
if (aliases) {
Set<MediaType> aliasSet = getMediaTypeAliases(mediaType, mimeTypes.getMediaTypeRegistry());
for(MediaType alias : aliasSet) {
GenericValue aliasValue = makeEntityMimeType(delegator, alias, mimeTypes,
mainValue.getString("description") + " (alias)",
getMimeTypeId(alias) + " (alias for " + getMimeTypeId(mediaType) + ")");
if (!missingOnly || UtilValidate.isEmpty(delegator.findOne("MimeType", true, UtilMisc.toMap("mimeTypeId", getMimeTypeId(alias))))) {
mimeTypeValues.add(aliasValue);
}
// SANITY CHECK
Set<MediaType> aliasAliasSet = getMediaTypeAliases(alias, mimeTypes.getMediaTypeRegistry());
if (aliasAliasSet.isEmpty()) {
Debug.logWarning("Tika: Sanity check failed: " + alias.toString() + " has no aliases (unlike its canonical form)", module);
} else if (aliasSet.size() != aliasAliasSet.size()) {
Debug.logWarning("Tika: Sanity check failed: " + alias.toString() + " has different number of aliases than its canonical form", module);
}
}
}
}
return mimeTypeValues;
}
示例14: testGetContentType
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
/** Unit tests for getContentType(String, String, byte[]) method. */
@Test
public void testGetContentType() throws Exception {
Content c = null;
Metadata p = new Metadata();
c = new Content("http://www.foo.com/", "http://www.foo.com/",
"".getBytes("UTF8"), "text/html; charset=UTF-8", p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/foo.html", "http://www.foo.com/",
"".getBytes("UTF8"), "", p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/foo.html", "http://www.foo.com/",
"".getBytes("UTF8"), null, p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/", "http://www.foo.com/",
"<html></html>".getBytes("UTF8"), "", p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/foo.html", "http://www.foo.com/",
"<html></html>".getBytes("UTF8"), "text/plain", p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/foo.png", "http://www.foo.com/",
"<html></html>".getBytes("UTF8"), "text/plain", p, conf);
Assert.assertEquals("text/html", c.getContentType());
c = new Content("http://www.foo.com/", "http://www.foo.com/",
"".getBytes("UTF8"), "", p, conf);
Assert.assertEquals(MimeTypes.OCTET_STREAM, c.getContentType());
c = new Content("http://www.foo.com/", "http://www.foo.com/",
"".getBytes("UTF8"), null, p, conf);
Assert.assertNotNull(c.getContentType());
}
示例15: getExtension
import org.apache.tika.mime.MimeTypes; //导入依赖的package包/类
static String getExtension(String contentType){
try {
return MimeTypes.getDefaultMimeTypes().forName(contentType).getExtension();
} catch (MimeTypeException e) {
//TODO find another exception
throw new RuntimeException(String.format("Unable to resolve content type '%s'", contentType), e);
}
}