本文整理匯總了Java中org.apache.tika.mime.MediaType.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaType.parse方法的具體用法?Java MediaType.parse怎麽用?Java MediaType.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.tika.mime.MediaType
的用法示例。
在下文中一共展示了MediaType.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: render
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@Override
protected void render(RenderingContext context)
{
ContentReader contentReader = context.makeContentReader();
String sourceMimeType = contentReader.getMimetype();
// Check that Tika supports the supplied file
AutoDetectParser p = new AutoDetectParser(tikaConfig);
MediaType sourceMediaType = MediaType.parse(sourceMimeType);
if(! p.getParsers().containsKey(sourceMediaType))
{
throw new RenditionServiceException(
"Source mime type of " + sourceMimeType +
" is not supported by Tika for HTML conversions"
);
}
// Make the HTML Version using Tika
// This will also extract out any images as found
generateHTML(p, context);
}
示例2: create
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@RequestMapping(method = POST)
@PreAuthorize("isAuthenticated()")
@ResponseBody
@ResponseStatus(CREATED)
public String create(
@RequestParam("assetData") final MultipartFile assetData
) throws IOException {
// Check duplicates
final GridFSDBFile file = this.gridFs.findOne(Query.query(Criteria.where("filename").is(assetData.getOriginalFilename())));
if (file != null) {
throw new DataIntegrityViolationException(String.format("Asset with name '%s' already exists", assetData.getOriginalFilename()));
} else {
try (InputStream usedStream = TikaInputStream.get(assetData.getInputStream())) {
MediaType mediaType = null;
try {
mediaType = MediaType.parse(tika.detect(usedStream, assetData.getOriginalFilename()));
} catch (IOException e) {
log.warn("Could not detect content type", e);
}
this.gridFs.store(assetData.getInputStream(), assetData.getOriginalFilename(), Optional.ofNullable(mediaType).map(MediaType::toString).orElse(null));
return assetData.getOriginalFilename();
}
}
}
示例3: detectOpenDocument
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* OpenDocument files, along with EPub files, have a mimetype
* entry in the root of their Zip file. This entry contains the
* mimetype of the overall file, stored as a single string.
*/
private static MediaType detectOpenDocument(ZipFile zip) {
try {
ZipArchiveEntry mimetype = zip.getEntry("mimetype");
if (mimetype != null) {
InputStream stream = zip.getInputStream(mimetype);
try {
return MediaType.parse(IOUtils.toString(stream, "UTF-8"));
} finally {
stream.close();
}
} else {
return null;
}
} catch (IOException e) {
return null;
}
}
示例4: addHtmlMetadata
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Adds a metadata setting from the HTML <head/> to the Tika metadata
* object. The name and value are normalized where possible.
*/
private void addHtmlMetadata(String name, String value) {
if (name == null || value == null) {
// ignore
} else if (name.equalsIgnoreCase("ICBM")) {
Matcher m = ICBM.matcher(value);
if (m.matches()) {
metadata.set("ICBM", m.group(1) + ", " + m.group(2));
metadata.set(Metadata.LATITUDE, m.group(1));
metadata.set(Metadata.LONGITUDE, m.group(2));
} else {
metadata.set("ICBM", value);
}
} else if (name.equalsIgnoreCase(Metadata.CONTENT_TYPE)){
MediaType type = MediaType.parse(value);
if (type != null) {
metadata.set(Metadata.CONTENT_TYPE, type.toString());
} else {
metadata.set(Metadata.CONTENT_TYPE, value);
}
} else {
metadata.set(name, value);
}
}
示例5: detectMediaType
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
public static MediaType detectMediaType(InputStream contentStream, @Nullable String fileName) {
try {
return MediaType.parse(tika.detect(contentStream, fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例6: UniversalEncodingListener
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
public UniversalEncodingListener(Metadata metadata) {
MediaType type = MediaType.parse(metadata.get(Metadata.CONTENT_TYPE));
if (type != null) {
hint = type.getParameters().get("charset");
}
if (hint == null) {
hint = metadata.get(Metadata.CONTENT_ENCODING);
}
}
示例7: typeBasedOnDetectedTypeAndExtension
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
private MediaType typeBasedOnDetectedTypeAndExtension(MediaType type, String filename)
{
if (filename != null && type != null)
{
String[] detectedAndPossibleTypes = new String[]
{
MIMETYPE_PDF, MIMETYPE_APPLICATION_ILLUSTRATOR,
MIMETYPE_APPLICATION_PS, MIMETYPE_APPLICATION_EPS
};
for (int i=detectedAndPossibleTypes.length-1; i>=0; i-=2)
{
String detectedType = detectedAndPossibleTypes[i-1];
if (detectedType.equals(type.toString()))
{
String possibleType = detectedAndPossibleTypes[i];
String extension = getExtension(possibleType);
if (filename.endsWith("."+extension))
{
type = MediaType.parse(possibleType);
break;
}
}
}
}
return type;
}
示例8: getMimetypeIfNotMatches
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Use Apache Tika to check if the mime type of the document really matches
* what it claims to be. This is typically used when a transformation or
* metadata extractions fails, and you want to know if someone has renamed a
* file and consequently it has the wrong mime type.
*
* @return Null if the mime type seems ok, otherwise the mime type it
* probably is
*/
public String getMimetypeIfNotMatches(ContentReader reader)
{
MediaType type = detectType(null, reader);
if (type == null)
{
// Tika doesn't know so we can't help, sorry...
return null;
}
// Is it a good match?
if (type.toString().equals(reader.getMimetype())) { return null; }
// Is it close?
MediaType claimed = MediaType.parse(reader.getMimetype());
if (tikaConfig.getMediaTypeRegistry().isSpecializationOf(claimed, type)
|| tikaConfig.getMediaTypeRegistry().isSpecializationOf(type, claimed))
{
// Probably close enough
return null;
}
// Check through known aliases of the type
SortedSet<MediaType> aliases = tikaConfig.getMediaTypeRegistry().getAliases(type);
for (MediaType alias : aliases)
{
String aliasType = alias.toString();
if (aliasType.equals(claimed.toString()))
{
return null;
}
}
// If we get here, then most likely the type is wrong
return type.toString();
}
示例9: asMediaType
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Makes a non-normalized tika MediaType instance (non-normalized means it may be an alias).
* Result NOT necessarily exists in the registry.
*/
public static MediaType asMediaType(String mediaType) {
return MediaType.parse(mediaType);
// this code returned null for aliases.
//MimeType mimeType = getMimeTypeForMediaTypeSafe(mediaType, getMimeTypeRegistry(), exact);
//return mimeType != null ? mimeType.getType() : null;
}
示例10: getHttpMediaType
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
MediaType getHttpMediaType(Header[] headers) {
MediaType httpMediaType = null;
for (Header h : headers) {
if (h.getName().equalsIgnoreCase("content-type")) {
String v = h.getValue();
httpMediaType = MediaType.parse(v);
}
}
return httpMediaType;
}
示例11: parseSiteMap
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Parse a sitemap, given the MIME type, the content bytes, and the URL.
* Note that contentType is assumed to be correct; in general it is more
* robust to use the method that doesn't take a contentType, but instead
* detects this using Tika.
*
* @param contentType
* MIME type of content
* @param content
* raw bytes of sitemap file
* @param url
* URL to sitemap file
* @return Extracted SiteMap/SiteMapIndex
* @throws UnknownFormatException
* if there is an error parsing the sitemap
* @throws IOException
* if there is an error reading in the site map
* {@link java.net.URL}
*/
public AbstractSiteMap parseSiteMap(String contentType, byte[] content, URL url) throws UnknownFormatException, IOException {
MediaType mediaType = MediaType.parse(contentType);
// Octet-stream is the father of all binary types
while (mediaType != null && !mediaType.equals(MediaType.OCTET_STREAM)) {
if (XML_MEDIA_TYPES.contains(mediaType)) {
return processXml(url, content);
} else if (TEXT_MEDIA_TYPES.contains(mediaType)) {
return processText(url, content);
} else if (GZ_MEDIA_TYPES.contains(mediaType)) {
InputStream decompressed;
MediaType embeddedType;
try {
decompressed = new GZIPInputStream(new ByteArrayInputStream(content));
embeddedType = MediaType.parse(TIKA.detect(decompressed));
} catch (Exception e) {
UnknownFormatException err = new UnknownFormatException("Failed to detect embedded MediaType of gzipped sitemap: " + url + ", caused by " + e);
err.initCause(e);
throw err;
}
if (XML_MEDIA_TYPES.contains(embeddedType)) {
return processGzippedXML(url, content);
} else if (TEXT_MEDIA_TYPES.contains(embeddedType)) {
// re-open decompressed stream and parse as text
decompressed = new GZIPInputStream(new ByteArrayInputStream(content));
return processText(url, decompressed);
} else if (GZ_MEDIA_TYPES.contains(embeddedType)) {
throw new UnknownFormatException("Can't parse gzip recursively: " + url);
}
throw new UnknownFormatException("Can't parse a gzipped sitemap with the embedded MediaType of: " + embeddedType + " (at: " + url + ")");
}
mediaType = MEDIA_TYPE_REGISTRY.getSupertype(mediaType); // Check
// parent
}
throw new UnknownFormatException("Can't parse a sitemap with the MediaType of: " + contentType + " (at: " + url + ")");
}
示例12: initMediaTypes
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Performs a one time intialization of Tika's Media-Type components and
* media type collection constants <br/>
* Please note that this is a private static method which is called once per
* CLASS (not per instance / object)
*/
private static void initMediaTypes() {
/* XML media types (and all aliases) */
XML_MEDIA_TYPES.add(APPLICATION_XML);
XML_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(APPLICATION_XML));
/* TEXT media types (and all aliases) */
TEXT_MEDIA_TYPES.add(TEXT_PLAIN);
TEXT_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(TEXT_PLAIN));
/* GZIP media types (and all aliases) */
MediaType gzipMediaType = MediaType.parse("application/gzip");
GZ_MEDIA_TYPES.add(gzipMediaType);
GZ_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(gzipMediaType));
}
示例13: getMimeTypeFromContentType
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
protected static String getMimeTypeFromContentType(String contentType) {
String result = "";
MediaType mt = MediaType.parse(contentType);
if (mt != null) {
result = mt.getType() + "/" + mt.getSubtype();
}
return result;
}
示例14: hasValidMimeType
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@Override
public boolean hasValidMimeType(FileUpload file) {
MediaType type = MediaType.parse(file.getContentType());
if (!"image".equalsIgnoreCase(type.getType())) {
return false;
}
boolean gif = "gif".equalsIgnoreCase(type.getSubtype());
boolean png = "png".equalsIgnoreCase(type.getSubtype());
boolean jpg = "jpeg".equalsIgnoreCase(type.getSubtype());
return gif || png || jpg;
}
示例15: detectOfficeOpenXML
import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
* Detects the type of an OfficeOpenXML (OOXML) file from
* opened Package
*/
public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
PackageRelationshipCollection core =
pkg.getRelationshipsByType(ExtractorFactory.CORE_DOCUMENT_REL);
if (core.size() != 1) {
// Invalid OOXML Package received
return null;
}
// Get the type of the core document part
PackagePart corePart = pkg.getPart(core.getRelationship(0));
String coreType = corePart.getContentType();
// Turn that into the type of the overall document
String docType = coreType.substring(0, coreType.lastIndexOf('.'));
// The Macro Enabled formats are a little special
if(docType.toLowerCase().endsWith("macroenabled")) {
docType = docType.toLowerCase() + ".12";
}
if(docType.toLowerCase().endsWith("macroenabledtemplate")) {
docType = MACRO_TEMPLATE_PATTERN.matcher(docType).replaceAll("macroenabled.12");
}
// Build the MediaType object and return
return MediaType.parse(docType);
}