Search in sources :

Example 1 with FileNameMap

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;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileNameMap(java.net.FileNameMap)

Example 2 with FileNameMap

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);
}
Also used : FileNameMap(java.net.FileNameMap)

Example 3 with FileNameMap

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
// '.'
}
Also used : FileNameMap(java.net.FileNameMap)

Example 4 with FileNameMap

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;
}
Also used : Category(com.agiletec.aps.system.services.category.Category) ArrayList(java.util.ArrayList) FileNameMap(java.net.FileNameMap) BaseResourceDataBean(com.agiletec.plugins.jacms.aps.system.services.resource.model.BaseResourceDataBean) File(java.io.File)

Example 5 with FileNameMap

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;
    }
}
Also used : FileNameMap(java.net.FileNameMap) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FileNameMap (java.net.FileNameMap)34 File (java.io.File)7 SimpleDateFormat (java.text.SimpleDateFormat)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 DigestException (com.openmeap.digest.DigestException)2 GlobalSettings (com.openmeap.model.dto.GlobalSettings)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Wrong (app.hongs.util.verify.Wrong)1 Category (com.agiletec.aps.system.services.category.Category)1 BaseResourceDataBean (com.agiletec.plugins.jacms.aps.system.services.resource.model.BaseResourceDataBean)1 ModelManager (com.openmeap.model.ModelManager)1 Application (com.openmeap.model.dto.Application)1 ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)1