本文整理了Java中java.net.URLConnection.guessContentTypeFromStream()
方法的一些代码示例,展示了URLConnection.guessContentTypeFromStream()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.guessContentTypeFromStream()
方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:guessContentTypeFromStream
[英]Determines the MIME-type of the resource represented by the input stream is by reading its first few characters.
[中]通过读取输入流的前几个字符来确定输入流表示的资源的MIME类型。
代码示例来源:origin: stackoverflow.com
InputStream is = new BufferedInputStream(new FileInputStream(file));
mimeType = URLConnection.guessContentTypeFromStream(is);
//...close stream
代码示例来源:origin: stackoverflow.com
byte[] content = ;
InputStream is = new BufferedInputStream(new ByteArrayInputStream(content));
String mimeType = URLConnection.guessContentTypeFromStream(is);
//...close stream
代码示例来源:origin: jphp-group/jphp
@Signature
public static String guessContentTypeFromStream(InputStream is) throws IOException {
return URLConnection.guessContentTypeFromStream(is);
}
代码示例来源:origin: robovm/robovm
/**
* Returns an object representing the content of the resource this {@code
* URLConnection} is connected to. First, it attempts to get the content
* type from the method {@code getContentType()} which looks at the response
* header field "Content-Type". If none is found it will guess the content
* type from the filename extension. If that fails the stream itself will be
* used to guess the content type.
*
* @return the content representing object.
* @throws IOException
* if an error occurs obtaining the content.
*/
public Object getContent() throws java.io.IOException {
if (!connected) {
connect();
}
if ((contentType = getContentType()) == null) {
if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
contentType = guessContentTypeFromStream(getInputStream());
}
}
if (contentType != null) {
return getContentHandler(contentType).getContent(this);
}
return null;
}
代码示例来源:origin: robovm/robovm
contentType = guessContentTypeFromStream(getInputStream());
代码示例来源:origin: RipMeApp/ripme
String fileExt = URLConnection.guessContentTypeFromStream(bis);
if (fileExt != null) {
fileExt = fileExt.replaceAll("image/", "");
代码示例来源:origin: foxinmy/weixin4j
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream(new File("/Users/jy/Downloads/test.mp4"));
System.err.println(getFileType(is));
System.err.println(URLConnection.guessContentTypeFromStream(is));
}
}
代码示例来源:origin: com.sun.mail/javax.mail
/**
* Determines the mimeType of a formatter from the getHead call.
* This could be made protected, or a new class could be created to do
* this type of conversion. Currently, this is only used for the body
* since the attachments are computed by filename.
* Package-private for unit testing.
* @param chunk any char sequence or null.
* @return return the mime type or null for text/plain.
*/
final String contentTypeOf(CharSequence chunk) {
if (!isEmpty(chunk)) {
final int MAX_CHARS = 25;
if (chunk.length() > MAX_CHARS) {
chunk = chunk.subSequence(0, MAX_CHARS);
}
try {
final String charset = getEncodingName();
final byte[] b = chunk.toString().getBytes(charset);
final ByteArrayInputStream in = new ByteArrayInputStream(b);
assert in.markSupported() : in.getClass().getName();
return URLConnection.guessContentTypeFromStream(in);
} catch (final IOException IOE) {
reportError(IOE.getMessage(), IOE, ErrorManager.FORMAT_FAILURE);
}
}
return null; //text/plain
}
代码示例来源:origin: camunda/camunda-bpm-platform
/**
* Determines the mimeType of a formatter from the getHead call.
* This could be made protected, or a new class could be created to do
* this type of conversion. Currently, this is only used for the body
* since the attachments are computed by filename.
* Package-private for unit testing.
* @param head any head string.
* @return return the mime type or null for text/plain.
*/
final String contentTypeOf(String head) {
if (!isEmpty(head)) {
final int MAX_CHARS = 25;
if (head.length() > MAX_CHARS) {
head = head.substring(0, MAX_CHARS);
}
try {
final String charset = getEncodingName();
final ByteArrayInputStream in
= new ByteArrayInputStream(head.getBytes(charset));
assert in.markSupported() : in.getClass().getName();
return URLConnection.guessContentTypeFromStream(in);
} catch (final IOException IOE) {
reportError(IOE.getMessage(), IOE, ErrorManager.FORMAT_FAILURE);
}
}
return null; //text/plain
}
代码示例来源:origin: com.atlassian.jira/jira-core
private String guessContentType(final InputStream inputStream) throws IOException
{
return URLConnection.guessContentTypeFromStream(inputStream) + "; charset=binary";
}
}
代码示例来源:origin: org.kie.workbench.stunner/kie-wb-common-stunner-backend-common
public static String guessContentType(final String fileName,
final InputStream stream) throws Exception {
final String contentType = URLConnection.guessContentTypeFromStream(stream);
if (null == contentType) {
final int index = fileName.lastIndexOf(".");
return index >= 0 ?
"image/" + fileName.substring(index + 1, fileName.length()) : null;
}
return contentType;
}
}
代码示例来源:origin: stackoverflow.com
Intent myIntent = new Intent(Intent.ACTION_VIEW);
String mime=URLConnection.guessContentTypeFromStream(new FileInputStream(item));
if(mime==null) mime=URLConnection.guessContentTypeFromName(item.getName());
myIntent.setDataAndType(Uri.fromFile(item), mime);
startActivity(myIntent);
代码示例来源:origin: com.github.bloodshura/shurax
public MultipartObject(@Nonnull Resource resource, @Nonnull String defaultContentType) {
String guessedType = null;
try (InputStream input = resource.newInputStream()) {
guessedType = URLConnection.guessContentTypeFromStream(input);
}
catch (IOException exception) {
// Just ignore.
}
this.contentType = guessedType != null ? guessedType : defaultContentType;
this.resource = resource;
}
代码示例来源:origin: com.foxinmy/weixin4j-base
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream(new File("/Users/jy/Downloads/test.mp4"));
System.err.println(getFileType(is));
System.err.println(URLConnection.guessContentTypeFromStream(is));
}
}
代码示例来源:origin: com.github.bloodshura/ignitium-core
public MultipartObject(@Nonnull Resource resource, @Nonnull String defaultContentType) {
String guessedType = null;
try (InputStream input = resource.newInputStream()) {
guessedType = URLConnection.guessContentTypeFromStream(input);
}
catch (IOException exception) {
// Just ignore.
}
this.contentType = guessedType != null ? guessedType : defaultContentType;
this.resource = resource;
}
代码示例来源:origin: com.xmlcalabash/xmlcalabash
public void addInput(String port, InputStream inputStream, String uri, Type type, String contentType) throws IOException {
inputStream = new BufferedInputStream(inputStream);
contentType = ((contentType == null) || "content/unknown".equals(contentType)) ? guessContentTypeFromStream(inputStream) : contentType;
contentType = ((contentType == null) || "content/unknown".equals(contentType)) ? guessContentTypeFromName(uri) : contentType;
curStep.addInput(port, inputStream, uri, type, contentType);
}
代码示例来源:origin: org.daisy.libs/com.xmlcalabash
public void addInput(String port, InputStream inputStream, String uri, Type type, String contentType) throws IOException {
inputStream = new BufferedInputStream(inputStream);
contentType = ((contentType == null) || "content/unknown".equals(contentType)) ? guessContentTypeFromStream(inputStream) : contentType;
contentType = ((contentType == null) || "content/unknown".equals(contentType)) ? guessContentTypeFromName(uri) : contentType;
curStep.addInput(port, inputStream, uri, type, contentType);
}
代码示例来源:origin: stackoverflow.com
InputStream is = new BufferedInputStream(new ByteArrayInputStream(myByteArray));
String mimeType = URLConnection.guessContentTypeFromStream(is);
if ( mimeType == null){
result.addError(new ObjectError("file", "Only SVG allowed!"));
}else if(mimeType != null && !mimeType.equalsIgnoreCase("application/xml")){
result.addError(new ObjectError("file", "Sorry Bro, only SVG allowed!"));
}
if (result.hasErrors()) {
// send back to edit form
model.addAttribute("uploadForm", data);
return "image/editPage";
}
代码示例来源:origin: stackoverflow.com
You can use the MimetypesFileTypeMap provided class from JAVA 6. This class is exclusively used to fetch the MIME type.
use it to fetch the MIME type as given below :
byte[] content = ;
InputStream is = new BufferedInputStream(new ByteArrayInputStream(content));
String mimeType = URLConnection.guessContentTypeFromStream(is);
For fetching from File you can use below code:
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mime = mimeTypesMap.getContentType(file);
代码示例来源:origin: org.jboss/jboss-vfs
public String getContentType() {
if (contentType != null) { return contentType; }
contentType = getFileNameMap().getContentTypeFor(getName());
if (contentType == null) {
try {
InputStream is = getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
contentType = java.net.URLConnection.guessContentTypeFromStream(bis);
bis.close();
} catch (IOException e) { /* ignore */ }
}
return contentType;
}
内容来源于网络,如有侵权,请联系作者删除!