Search in sources :

Example 21 with FileNameMap

use of java.net.FileNameMap in project Bytecoder by mirkosertic.

the class AbstractFileTypeDetector method probeContentType.

/**
 * Invokes the appropriate probe method to guess a file's content type,
 * and checks that the content type's syntax is valid.
 */
@Override
public final String probeContentType(Path file) throws IOException {
    if (file == null)
        throw new NullPointerException("'file' is null");
    String result = implProbeContentType(file);
    // Fall back to content types property.
    if (result == null) {
        Path fileName = file.getFileName();
        if (fileName != null) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            result = fileNameMap.getContentTypeFor(fileName.toString());
        }
    }
    return (result == null) ? null : parse(result);
}
Also used : Path(java.nio.file.Path) FileNameMap(java.net.FileNameMap)

Example 22 with FileNameMap

use of java.net.FileNameMap in project Bytecoder by mirkosertic.

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)

Example 23 with FileNameMap

use of java.net.FileNameMap in project My-MVP by REBOOTERS.

the class HttpDownload method run.

@Override
public void run() {
    super.run();
    try {
        URL url = new URL(this.url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(20000);
        connection.setReadTimeout(20000);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        FileNameMap map = HttpURLConnection.getFileNameMap();
        System.err.println("the map is " + map.toString());
        File file = new File("myFile.pdf");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] buffer = new byte[2048];
        int len = 0;
        int fileLength = connection.getContentLength();
        int temp = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
            temp = temp + len;
            System.err.println("the len is " + temp * 100 / fileLength);
        }
        fileOutputStream.flush();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileNameMap(java.net.FileNameMap) File(java.io.File) URL(java.net.URL)

Example 24 with FileNameMap

use of java.net.FileNameMap in project BaseProject by feer921.

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 25 with FileNameMap

use of java.net.FileNameMap in project TrebleShot by genonbeta.

the class FileUtils method getFileContentType.

public static String getFileContentType(String fileUrl) {
    FileNameMap nameMap = URLConnection.getFileNameMap();
    String fileType = nameMap.getContentTypeFor(fileUrl);
    return (fileType == null) ? "*/*" : fileType;
}
Also used : FileNameMap(java.net.FileNameMap)

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