use of java.net.FileNameMap in project okhttputils by hongyangAndroid.
the class PostFormRequest method guessMimeType.
private String guessMimeType(String path) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentTypeFor = null;
try {
contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (contentTypeFor == null) {
contentTypeFor = "application/octet-stream";
}
return contentTypeFor;
}
use of java.net.FileNameMap in project okhttp-OkGo by jeasonlzy.
the class HttpParams method guessMimeType.
private MediaType guessMimeType(String path) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
//解决文件名中含有#号异常的问题
path = path.replace("#", "");
String contentType = fileNameMap.getContentTypeFor(path);
if (contentType == null) {
contentType = "application/octet-stream";
}
return MediaType.parse(contentType);
}
use of java.net.FileNameMap in project robovm by robovm.
the class URLConnectionTest method test_getFileNameMap.
/**
* {@link java.net.URLConnection#getFileNameMap()}
*/
public void test_getFileNameMap() {
// Tests for the standard MIME types -- users may override these
// in their JRE
FileNameMap mapOld = URLConnection.getFileNameMap();
try {
// These types are defaulted
assertEquals("text/html", mapOld.getContentTypeFor(".htm"));
assertEquals("text/html", mapOld.getContentTypeFor(".html"));
assertEquals("text/plain", mapOld.getContentTypeFor(".text"));
assertEquals("text/plain", mapOld.getContentTypeFor(".txt"));
// These types come from the properties file :
// not black-box testing. Special tests moved to setContentType
/*
assertEquals("application/pdf", map.getContentTypeFor(".pdf"));
assertEquals("application/zip", map.getContentTypeFor(".zip"));
assertEquals("image/gif", map.getContentTypeFor("gif"));
*/
URLConnection.setFileNameMap(new FileNameMap() {
public String getContentTypeFor(String fileName) {
return "Spam!";
}
});
assertEquals("Incorrect FileNameMap returned", "Spam!", URLConnection.getFileNameMap().getContentTypeFor(null));
} finally {
// unset the map so other tests don't fail
URLConnection.setFileNameMap(mapOld);
}
// RI fails since it does not support fileName that does not begin with
// '.'
}
use of java.net.FileNameMap in project entando-core by entando.
the class JAXBResource method createBataBean.
public BaseResourceDataBean createBataBean(ICategoryManager categoryManager) throws Throwable {
BaseResourceDataBean bean = new BaseResourceDataBean();
if (null != this.getCategories()) {
List<Category> categories = new ArrayList<>();
for (int i = 0; i < this.getCategories().size(); i++) {
String categoryCode = this.getCategories().get(i);
Category category = categoryManager.getCategory(categoryCode);
if (null != category) {
categories.add(category);
}
}
bean.setCategories(categories);
}
bean.setDescr(this.getDescription());
bean.setFileName(this.getFileName());
bean.setMainGroup(this.getMainGroup());
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(this.getFileName());
bean.setMimeType(mimeType);
bean.setResourceType(this.getTypeCode());
bean.setResourceId(this.getId());
if (null != this.getBase64()) {
File file = this.byteArrayToFile();
bean.setFile(file);
}
return bean;
}
use of java.net.FileNameMap in project jdk8u_jdk by JetBrains.
the class FileURLConnection method initializeHeaders.
private void initializeHeaders() {
try {
connect();
exists = file.exists();
} catch (IOException e) {
}
if (!initializedHeaders || !exists) {
length = file.length();
lastModified = file.lastModified();
if (!isDirectory) {
FileNameMap map = java.net.URLConnection.getFileNameMap();
contentType = map.getContentTypeFor(filename);
if (contentType != null) {
properties.add(CONTENT_TYPE, contentType);
}
properties.add(CONTENT_LENGTH, String.valueOf(length));
/*
* Format the last-modified field into the preferred
* Internet standard - ie: fixed-length subset of that
* defined by RFC 1123
*/
if (lastModified != 0) {
Date date = new Date(lastModified);
SimpleDateFormat fo = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
fo.setTimeZone(TimeZone.getTimeZone("GMT"));
properties.add(LAST_MODIFIED, fo.format(date));
}
} else {
properties.add(CONTENT_TYPE, TEXT_PLAIN);
}
initializedHeaders = true;
}
}
Aggregations