本文整理汇总了Java中javax.activation.MimetypesFileTypeMap.getContentType方法的典型用法代码示例。如果您正苦于以下问题:Java MimetypesFileTypeMap.getContentType方法的具体用法?Java MimetypesFileTypeMap.getContentType怎么用?Java MimetypesFileTypeMap.getContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.activation.MimetypesFileTypeMap
的用法示例。
在下文中一共展示了MimetypesFileTypeMap.getContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setContentTypeHeader
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Sets the content type header for the HTTP Response
*
* @param response
* HTTP response
* @param file
* file to extract content type
*/
private static void setContentTypeHeader(HttpResponse response, String path) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
////////////////////////////////////////
//NOTE: this does not work and behaves different between Java releases!!!!!
///////////////////////////////////////
String contentType = mimeTypesMap.getContentType(path);
//patching mistake from the above broken code.
if (path.endsWith("css")) {
contentType = "text/css";
} else if (path.endsWith("html")) {
contentType = "text/html";
} else if (path.endsWith("jpg")) {
contentType = "image/jpeg";
}
response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
}
示例2: isImage
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Obtains a files MIME type and returns true if it is of type image
* @param fileName
* @param output
* @return true if an image; false otherwise
*/
public static boolean isImage(String fileName, boolean output) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(fileName);
if(output){
System.out.println("Validating input file...");
System.out.println("File type: "+mimeType);
}
if(mimeType.startsWith("image")){
return true;
}else{
if(output){
System.out.println("Invalid file type input. Please enter an image");
}
return false;
}
}
示例3: setContentTypeHeader
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Sets the content type header for the HTTP Response
*
* @param response
* HTTP response
* @param file
* file to extract content type
*/
private static void setContentTypeHeader(HttpResponse response, String path) {
String contentType = null;
if(path.endsWith(".css")) {
contentType = "text/css";
}
else if(path.endsWith(".js")) {
contentType = "application/javascript";
}
else if(path.endsWith(".html")) {
contentType = "text/html";
}
else {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
contentType = mimeTypesMap.getContentType(path);
}
response.headers().set(CONTENT_TYPE, contentType);
}
示例4: getMIMEType
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Return the mimetype of the file depending of his extension and the mime.types file
*
* @param strFilename
* the file name
* @return the file mime type
*/
public static String getMIMEType( String strFilename )
{
try
{
MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap( AppPathService.getWebAppPath( ) + File.separator + FILE_MIME_TYPE );
return mimeTypeMap.getContentType( strFilename.toLowerCase( ) );
}
catch( IOException e )
{
AppLogService.error( e );
return DEFAULT_MIME_TYPE;
}
}
示例5: getKnownContentTypes
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
*
*
* @return
*/
private String[] getKnownContentTypes() {
List<String> knownContentTypes = new ArrayList<>();
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
for (String supportedImageType : ImageIO.getReaderFileSuffixes()) {
String name = String.format("a.%s", supportedImageType);
String contentType = map.getContentType(name);
if (!contentType.equals(MediaType.APPLICATION_OCTET_STREAM)) {
knownContentTypes.add(contentType);
}
}
return knownContentTypes.toArray(new String[knownContentTypes.size()]);
}
示例6: isImage
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
public static boolean isImage(File image) {
MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
mimetypesFileTypeMap.addMimeTypes("image png tif jpg jpeg bmp");
String mimeType = mimetypesFileTypeMap.getContentType(image);
String type = mimeType.split("/")[0];
return type.equals("image");
}
示例7: getMimeType
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Determines the mime type for the file given by its name.
*
* @param f
* the name of the file
* @return the mime type of the given file
*/
public static String getMimeType(String f) {
if (f == null) {
return "application/octet-stream";
}
MimetypesFileTypeMap mTypes = new MimetypesFileTypeMap();
return mTypes.getContentType(f.toLowerCase(Locale.ROOT));
}
示例8: convertToSpreedSheet
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
public static boolean convertToSpreedSheet(Drive drive,
String path) {
com.google.api.services.drive.model.File fileMetaData = new com.google.api.services.drive.model.File();
java.io.File file = new java.io.File(path);
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
fileMetaData.setName(file.getName());
String googleSheetMimeType = "application/vnd.google-apps.spreadsheet";
fileMetaData.setMimeType(googleSheetMimeType);
FileContent mediaContent = new FileContent(mimeTypesMap.getContentType(file), file);
com.google.api.services.drive.model.File f = null;
Drive.Files.Create create = null;
try {
System.out.println("Uploading file " + file.getName());
create = drive.files().create(fileMetaData, mediaContent)
.setFields("id, parents, mimeType, webViewLink");
create.getMediaHttpUploader().setProgressListener(new FileUpdateProgressListener());
// Using default chunk size of 10MB.
create.getMediaHttpUploader().setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE);
f = create.execute();
} catch (IOException e) {
e.printStackTrace();
return false;
}
System.out.println("File ID: " + f.getId() + " Parent: " + f.getParents().toString()
+ " MimeType: " + f.getMimeType() + " link: " + f.getWebViewLink());
return true;
}
示例9: getNext
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Fetches the next piece of content of type T and returns it. This method can be safely invoked until
* complete() returns false. From which on this method will return null.
*
* @return Content of type T.
*/
@Override
public Mesh getNext() {
try {
MimetypesFileTypeMap filetypes = new MimetypesFileTypeMap("mime.types");
String contenttype = filetypes.getContentType(this.inputFile.toFile());
/* Try to detach decoder from the list of cached decoders. */
Decoder<Mesh> decoder = this.cachedDecoders.get(contenttype);
/* If decoder is null, create a new one. */
if (decoder == null) {
decoder = decoderForContenttype(contenttype);
}
/* If decoder is still null, return an emtpy Mesh. */
if (decoder == null) {
LOGGER.warn("Could not find mesh decoder for provided contenttype {}.", contenttype);
return Mesh.EMPTY;
} else {
this.cachedDecoders.put(contenttype, decoder);
}
/* Initialize the decoder and return the decoded mesh. */
decoder.init(this.inputFile, null);
Mesh mesh = decoder.getNext();
this.complete.set(true);
return mesh;
} catch (IOException e) {
LOGGER.error("Could not decode mesh file {} due to an IO exception ({})", this.inputFile.toString(), LogHelper.getStackTrace(e));
this.complete.set(true);
return null;
}
}
示例10: convert
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Converts a single file to a QueryContainer.
*
* @param path Path the file that should be converted.
* @return QueryContainer for the specified file.
*/
@Override
public QueryContainer convert(Path path) {
try {
MimetypesFileTypeMap filetypes = new MimetypesFileTypeMap("mime.types");
String contenttype = filetypes.getContentType(path.toFile());
/* Try to detach decoder from the list of cached decoders. */
Decoder<Mesh> decoder = this.cachedDecoders.get(contenttype);
/* If decoder is null, create a new one. */
if (decoder == null) {
decoder = decoderForContenttype(contenttype);
}
/* If decoder is still null, return an emtpy Mesh. */
if (decoder == null) {
LOGGER.warn("Could not find mesh decoder for provided contenttype {}.", contenttype);
return null;
} else {
this.cachedDecoders.put(contenttype, decoder);
}
/* Initialize the decoder and return the decoded mesh. */
decoder.init(path, null);
Mesh mesh = decoder.getNext();
return new ModelQueryContainer(mesh);
} catch (IOException e) {
LOGGER.error("Could not decode mesh file {} due to an IO exception ({})", path.toString(), LogHelper.getStackTrace(e));
return null;
}
}
示例11: setContentTypeHeader
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* This will set the content types of files. If you want to support any
* files add the content type and corresponding file extension here.
*
* @param response
* @param file
*/
private static void setContentTypeHeader(HttpResponse response, File file) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
mimeTypesMap.addMimeTypes("image png tif jpg jpeg bmp");
mimeTypesMap.addMimeTypes("text/plain txt");
mimeTypesMap.addMimeTypes("application/pdf pdf");
String mimeType = mimeTypesMap.getContentType(file);
response.headers().set(CONTENT_TYPE, mimeType);
}
示例12: makeContentResource
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
private ContentResource makeContentResource(String filename) {
AttachmentHelper attachmentHelper = new AttachmentHelper();
StringBuffer fullFilePath = new StringBuffer(unzipLocation);
fullFilePath.append("/");
fullFilePath.append(filename);
MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
String contentType = mimetypesFileTypeMap.getContentType(filename);
ContentResource contentResource = attachmentHelper.createContentResource(fullFilePath.toString(), filename, contentType);
return contentResource;
}
示例13: getIcon
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
@Override
public ResponseEntity<InputStreamResource> getIcon(String definitionName, DefinitionImageType imageType) {
notNull(definitionName, "Definition name cannot be null.");
notNull(imageType, "Definition image type cannot be null.");
final Definition<?> definition = propertiesHelpers.getDefinition(definitionName);
notNull(definition, "Could not find definition of name %s", definitionName);
// Undefined and missing icon resources are simply 404.
String imagePath = definition.getImagePath(imageType);
if (imagePath == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
InputStream is = definition.getClass().getResourceAsStream(imagePath);
if (is == null) {
log.info("The image type %s should exist for %s at %s, but is missing. "
+ "The component should provide this resource.", imageType, definitionName, imagePath);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// At this point, we have enough information for a correct response.
ResponseEntity.BodyBuilder response = ResponseEntity.ok();
// Add the content type if it can be determined.
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String contentType = mimeTypesMap.getContentType(imagePath);
if (contentType != null) {
response = response.contentType(MediaType.parseMediaType(contentType));
}
return response.body(new InputStreamResource(is));
}
示例14: getMimetype
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
public String getMimetype(File inputFile) {
try {
return getMatch(inputFile).getMimeType();
} catch (MagicMatchNotFoundException e) {
LOGGER.debug("Failed to get Mime Type", e);
final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
return mimeTypesMap.getContentType(inputFile);
}
}
示例15: getContentType
import javax.activation.MimetypesFileTypeMap; //导入方法依赖的package包/类
/**
* Return the content-type of a file, according to it extension.
* @param file the file.
* @return the content-type.
*/
private static String getContentType(final File file) {
final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
final String contentType = mimeTypesMap.getContentType(file);
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
}