本文整理汇总了Java中eu.medsea.mimeutil.MimeType.toString方法的典型用法代码示例。如果您正苦于以下问题:Java MimeType.toString方法的具体用法?Java MimeType.toString怎么用?Java MimeType.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eu.medsea.mimeutil.MimeType
的用法示例。
在下文中一共展示了MimeType.toString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCorrectedMimeSpecificity
import eu.medsea.mimeutil.MimeType; //导入方法依赖的package包/类
/**
* Get specificity of mime types with generic types forced to low values
*
* <p>"application/octet-stream" is forced to -1. "text/plain" is forced to 0. All other mime
* types return the specificity reported by mimeType itself.
*
* @param mimeType The mimeType to get the corrected specificity for.
* @return The corrected specificity.
*/
private int getCorrectedMimeSpecificity(MimeType mimeType) {
// Although the documentation of MimeType's getSpecificity claims that for
// example "application/octet-stream" always has a specificity of 0, it
// effectively returns 1 for us. This causes problems when trying to get
// the correct mime type via sorting. For example in
// [application/octet-stream, image/x-icon] both mime types come with
// specificity 1 for us. Hence, getMimeType below may end up using
// application/octet-stream instead of the more specific image/x-icon.
// Therefore, we have to force the specificity of generic types below the
// default of 1.
//
final String mimeTypeStr = mimeType.toString();
if (mimeTypeStr.equals("application/octet-stream")) {
return -1;
}
if (mimeTypeStr.equals("text/plain")) {
return 0;
}
return mimeType.getSpecificity();
}
示例2: mimeTypeGet2
import eu.medsea.mimeutil.MimeType; //导入方法依赖的package包/类
public static String mimeTypeGet2(Context context, String filename) {
AppState.logX(TAG, String.format("mimeTypeGet2: filename = %s", filename));
MimeType mimeType = UNKNOWN_MIME_TYPE;
AppState.logX(TAG, "mimeTypeGet2: 1");
File file = new File(filename);
AppState.logX(TAG, "mimeTypeGet2: 2");
if (file.isDirectory()) {
AppState.logX(TAG, "mimeTypeGet2: 3");
mimeType = MimeUtil2.DIRECTORY_MIME_TYPE;
AppState.logX(TAG, "mimeTypeGet2: 4");
} else {
AppState.logX(TAG, "mimeTypeGet2: 5");
MagicMimeMimeDetector detector = new MagicMimeMimeDetector(context);
AppState.logX(TAG, "mimeTypeGet2: 6");
Collection<MimeType> mimeTypes = new ArrayList<MimeType>();
AppState.logX(TAG, "mimeTypeGet2: 7");
mimeTypes.addAll(detector.getMimeTypesFileName(filename));
AppState.logX(TAG, String.format("mimeTypeGet2: 8: mimeType.size = %d", mimeTypes.size()));
mimeTypes.remove(UNKNOWN_MIME_TYPE);
AppState.logX(TAG, "mimeTypeGet2: 9");
mimeType = MimeUtil2.getMostSpecificMimeType(mimeTypes);
AppState.logX(TAG, "mimeTypeGet2: 10");
}
AppState.logX(TAG, String.format("mimeTypeGet2: mimeType = %s", mimeType != null ? mimeType.toString() :
"null"));
return mimeType != null ? mimeType.toString() : context.getString(R.string.mime_file_text);
}
示例3: doGet
import eu.medsea.mimeutil.MimeType; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Extract the filename from the request
String resourcePath = request.getPathInfo(); // Is already relative to /static!
if (request.getRequestURI().endsWith("/clippy.swf")) {
resourcePath = "/clippy.swf";
}
if (Strings.isNullOrEmpty(resourcePath)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// Must not contain any navigation
String[] segments = resourcePath.substring(1).split("/");
for (String segment : segments) {
if (segment.equals("..")) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Invalid path");
return;
}
}
String fileName = segments[segments.length - 1];
if (Strings.isNullOrEmpty(fileName)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// Restrict to the few known subdirectories.
if (segments.length > 1 && !ALLOWED_SUBDIRECTORIES.contains(segments[0])) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} else if (segments.length == 1 && !ALLOWED_FILE_NAMES.matcher(fileName).matches()) {
// Only allow the known filetypes: we have only png, css, js, swf, and gitblit.properties.
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// We just happen to know that all our static data files are small, so it's OK to read them fully into memory
byte[] bytes = null;
try (InputStream data = getClass().getResourceAsStream(resourcePath)) {
if (data == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
bytes = IO.readWholeStream(data, 0).array();
}
if (bytes == null) {
// Should not occur since we don't catch the possible IOException above. Nevertheless, let's be paranoid.
bytes = new byte[0];
}
String contentType = null;
// Compare https://gerrit-review.googlesource.com/#/c/67000/
if (fileName.toLowerCase().endsWith(".js")) {
contentType = "application/javascript";
} else if (fileName.toLowerCase().endsWith(".css")) {
contentType = "text/css";
} else {
MimeType mimeType = mimeDetector.getMimeType(fileName, bytes);
contentType = mimeType != null ? mimeType.toString() : "application/octet-stream";
}
response.setContentType(contentType);
long lastModified = getLastModified(request);
if (lastModified > 0) {
response.setDateHeader("Last-Modified", lastModified);
}
response.setHeader("Content-Length", Integer.toString(bytes.length));
try (OutputStream out = response.getOutputStream()) {
out.write(bytes, 0, bytes.length);
}
}