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);
}
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;
}
}
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();
}
}
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);
}
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;
}
Aggregations