use of javax.activation.MimetypesFileTypeMap in project oxTrust by GluuFederation.
the class ImageRepository method initFileTypesMap.
public void initFileTypesMap() throws Exception {
fileTypeMap = MimetypesFileTypeMap.getDefaultFileTypeMap();
InputStream is = ImageRepository.class.getClassLoader().getResourceAsStream("META-INF/mimetypes-gluu.default");
try {
if (is != null) {
fileTypeMap = new MimetypesFileTypeMap(is);
}
} catch (Exception ex) {
log.error("Failed to load file types map. Using default one.", ex);
fileTypeMap = new MimetypesFileTypeMap();
} finally {
IOUtils.closeQuietly(is);
}
}
use of javax.activation.MimetypesFileTypeMap in project CodeUtils by boredream.
the class LeanCloudHttpUtils method postFile.
public static String postFile(String url, File file) throws Exception {
// LeanCloud上传限制, 最多1秒1个
Thread.sleep(1000);
HashMap<String, String> map = getHeaderMap();
map.put("Content-Type", new MimetypesFileTypeMap().getContentType(file));
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(file));
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
int len;
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(file);
while ((len = fis.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of javax.activation.MimetypesFileTypeMap in project HongsCORE by ihongs.
the class FileAction method search.
@Override
@Action("search")
public void search(ActionHelper helper) throws HongsException {
MimetypesFileTypeMap nmap = new MimetypesFileTypeMap();
CoreLocale lang = CoreLocale.getInstance("manage");
String type = helper.getParameter("type");
String path = helper.getParameter("path");
String pxth = path;
File file;
// 根目录
if ("".equals(path) || "/".equals(path)) {
if ("file".equals(type)) {
helper.reply(Synt.mapOf("page", Synt.mapOf("ern", 1), "list", new ArrayList()));
} else {
helper.reply(Synt.mapOf("page", Synt.mapOf("ern", 0), "list", ROOT_LIST));
}
return;
}
if (path == null) {
helper.fault(lang.translate("core.manage.file.path.required"));
return;
}
path = realPath(path);
if (path == null) {
helper.fault(lang.translate("core.manage.file.path.is.error"));
return;
}
file = new File(path);
if (!file.exists()) {
helper.fault(lang.translate("core.manage.file.path.is.not.exist"));
return;
}
byte t = 0;
if ("dir".equals(type)) {
t = 1;
} else if ("file".equals(type)) {
t = 2;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
Set<Map> filez;
// 排序规则, 默认按名称排序
final boolean ds;
String ob = helper.getParameter("ob");
if (ob != null && ob.startsWith("-")) {
ob = ob.substring(1);
ds = true;
} else {
ds = false;
}
if ("type".equals(ob)) {
filez = new TreeSet(new Comparator<Map>() {
@Override
public int compare(Map f1, Map f2) {
String t1 = (String) f1.get("type");
String t2 = (String) f2.get("type");
byte s1 = Synt.declare(TYPE_SORT.get(t1), (byte) 0);
byte s2 = Synt.declare(TYPE_SORT.get(t2), (byte) 0);
if (s1 != s2)
if (ds)
return s1 < s2 ? 1 : -1;
else
return s1 > s2 ? 1 : -1;
String n1 = (String) f1.get("name");
String n2 = (String) f2.get("name");
if (ds)
return n2.compareTo(n1);
else
return n1.compareTo(n2);
}
});
} else if ("size".equals(ob)) {
filez = new TreeSet(new Comparator<Map>() {
@Override
public int compare(Map f1, Map f2) {
long s1 = Synt.declare(f1.get("size"), 0L);
long s2 = Synt.declare(f2.get("size"), 0L);
if (s1 != s2)
if (ds)
return s1 < s2 ? 1 : -1;
else
return s1 > s2 ? 1 : -1;
String n1 = (String) f1.get("name");
String n2 = (String) f2.get("name");
if (ds)
return n2.compareTo(n1);
else
return n1.compareTo(n2);
}
});
} else {
filez = new TreeSet(new Comparator<Map>() {
@Override
public int compare(Map f1, Map f2) {
String n1 = (String) f1.get("name");
String n2 = (String) f2.get("name");
if (ds)
return n2.compareTo(n1);
else
return n1.compareTo(n2);
}
});
}
for (File item : files) {
if (t == 1) {
if (!item.isDirectory()) {
continue;
}
} else if (t == 2) {
if (!item.isFile()) {
continue;
}
}
String name = item.getName();
String extn = name.replaceFirst("^.*/", "");
String mime = nmap.getContentType(extn);
Map xxxx = new HashMap();
xxxx.put("path", pxth + "/" + name);
xxxx.put("name", name);
xxxx.put("mime", mime);
xxxx.put("type", item.isDirectory() ? "dir" : (isTextFile(item) ? "txt" : "bin"));
xxxx.put("size", item.isDirectory() ? item.list().length : item.length());
xxxx.put("mtime", item.lastModified());
filez.add(xxxx);
}
Map rsp = new HashMap();
rsp.put("list", filez);
rsp.put("page", Synt.mapOf("ern", filez.size() > 0 ? 0 : 1));
helper.reply(rsp);
} else if (isTextFile(file)) {
String name = file.getName();
String extn = name.replaceFirst("^.*/", "");
String mime = nmap.getContentType(extn);
Map xxxx = new HashMap();
xxxx.put("path", pxth);
xxxx.put("name", name);
xxxx.put("mime", mime);
xxxx.put("type", "txt");
xxxx.put("size", file.length());
xxxx.put("text", readFile(file));
xxxx.put("mtime", file.lastModified());
helper.reply("", xxxx);
} else {
helper.fault(lang.translate("core.manage.file.unsupported"));
}
}
use of javax.activation.MimetypesFileTypeMap in project ff4j by ff4j.
the class StaticResourceController method serveFont.
/*
* Load font files
*/
private void serveFont(HttpServletResponse res, String pathInfo, String resourceName) throws IOException {
try {
if (!fontFiles.containsKey(resourceName)) {
fontFiles.put(resourceName, FileUtils.loadFileAsByteArray(pathInfo));
}
MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
res.setContentType(mimetypesFileTypeMap.getContentType(resourceName));
res.getOutputStream().write(fontFiles.get(resourceName));
} catch (FileNotFoundException fnf) {
notFound(res, "fontFiles " + pathInfo + "(" + resourceName + ")");
}
}
use of javax.activation.MimetypesFileTypeMap in project aliyun-oss-java-sdk by aliyun.
the class PostObjectSample method formUpload.
private static String formUpload(String urlStr, Map<String, String> formFields, String localFile) throws Exception {
String res = "";
HttpURLConnection conn = null;
String boundary = "9431149156168";
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (formFields != null) {
StringBuffer strBuf = new StringBuffer();
Iterator<Entry<String, String>> iter = formFields.entrySet().iterator();
int i = 0;
while (iter.hasNext()) {
Entry<String, String> entry = iter.next();
String inputName = entry.getKey();
String inputValue = entry.getValue();
if (inputValue == null) {
continue;
}
if (i == 0) {
strBuf.append("--").append(boundary).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
} else {
strBuf.append("\r\n").append("--").append(boundary).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
i++;
}
out.write(strBuf.toString().getBytes());
}
// file
File file = new File(localFile);
String filename = file.getName();
String contentType = new MimetypesFileTypeMap().getContentType(file);
if (contentType == null || contentType.equals("")) {
contentType = "application/octet-stream";
}
StringBuffer strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(boundary).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"file\"; " + "filename=\"" + filename + "\"\r\n");
strBuf.append("Content-Type: " + contentType + "\r\n\r\n");
out.write(strBuf.toString().getBytes());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] endData = ("\r\n--" + boundary + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// Gets the file data
strBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
System.err.println("Send post request exception: " + e);
throw e;
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
}
Aggregations