List of usage examples for java.net URLConnection guessContentTypeFromStream
public static String guessContentTypeFromStream(InputStream is) throws IOException
From source file:com.gargoylesoftware.htmlunit.WebClient.java
/** * Tries to guess the content type of the file.<br> * This utility could be located in an helper class but we can compare this functionality * for instance with the "Helper Applications" settings of Mozilla and therefore see it as a * property of the "browser"./*from www . j av a 2 s . c o m*/ * @param file the file * @return "application/octet-stream" if nothing could be guessed */ public String guessContentType(final File file) { String contentType = URLConnection.guessContentTypeFromName(file.getName()); if (file.getName().endsWith(".xhtml")) { // Java's mime type map doesn't know about XHTML files (at least in Sun JDK5). contentType = "application/xhtml+xml"; } if (contentType == null) { InputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(file)); contentType = URLConnection.guessContentTypeFromStream(inputStream); } catch (final IOException e) { // Ignore silently. } finally { IOUtils.closeQuietly(inputStream); } } if (contentType == null) { if (file.getName().endsWith(".js")) { contentType = "text/javascript"; } else { contentType = "application/octet-stream"; } } return contentType; }
From source file:edu.cmu.cylab.starslinger.view.HomeActivity.java
private long getOutStreamSizeAndData(Uri uri, String contentType) throws IOException { String name = null;/*from w ww . j a v a 2 s .co m*/ try { Cursor c = getContentResolver().query(uri, new String[] { MediaColumns.DISPLAY_NAME }, null, null, null); if (c != null) { try { if (c.moveToFirst()) { name = c.getString(c.getColumnIndex(MediaColumns.DISPLAY_NAME)); } } finally { c.close(); } } } catch (IllegalArgumentException e) { // column may not exist } long size = -1; try { Cursor c = getContentResolver().query(uri, new String[] { MediaColumns.SIZE }, null, null, null); if (c != null) { try { if (c.moveToFirst()) { size = c.getInt(c.getColumnIndex(MediaColumns.SIZE)); } } finally { c.close(); } } } catch (IllegalArgumentException e) { // column may not exist } String data = null; try { Cursor c = getContentResolver().query(uri, new String[] { MediaColumns.DATA }, null, null, null); if (c != null) { try { if (c.moveToFirst()) { data = c.getString(c.getColumnIndex(MediaColumns.DATA)); } } finally { c.close(); } } } catch (IllegalArgumentException e) { // column may not exist } if (name == null) { name = uri.getLastPathSegment(); } File f = null; if (size <= 0) { String uriString = uri.toString(); if (uriString.startsWith("file://")) { MyLog.v(TAG, uriString.substring("file://".length())); f = new File(uriString.substring("file://".length())); size = f.length(); } else { MyLog.v(TAG, "not a file: " + uriString); } } ContentResolver cr = getContentResolver(); InputStream is; // read file bytes try { is = cr.openInputStream(uri); } catch (FileNotFoundException e) { if (!TextUtils.isEmpty(data)) { is = new FileInputStream(data); } else { return -1; // unable to load file at all } } if ((contentType != null) && (contentType.indexOf('*') != -1)) { contentType = getContentResolver().getType(uri); } if (contentType == null) { contentType = URLConnection.guessContentTypeFromStream(is); if (contentType == null) { String extension = SSUtil.getFileExtensionOnly(name); contentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (contentType == null) { contentType = SafeSlingerConfig.MIMETYPE_OPEN_ATTACH_DEF; } } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; while (is.read(buf) > -1) { baos.write(buf); } baos.flush(); final byte[] fileBytes = baos.toByteArray(); DraftData d = DraftData.INSTANCE; d.setFileData(fileBytes); d.setFileSize(fileBytes.length); d.setFileType(contentType); d.setFileName(name); if (f != null && f.exists()) { d.setFileDir(f.getAbsolutePath()); } else if (!TextUtils.isEmpty(data)) { d.setFileDir(new File(data).getAbsolutePath()); } return d.getFileSize(); }
From source file:edu.cmu.cylab.starslinger.view.HomeActivity.java
private void doLoadAttachment(String path) throws FileNotFoundException { File phy = new File(path); // physical File vir = new File(path); // virtual, change if needed DraftData d = DraftData.INSTANCE;//from w w w . j a va2 s .c o m try { FileInputStream is = new FileInputStream(phy.getAbsolutePath()); try { byte[] outFileData = new byte[is.available()]; is.read(outFileData); d.setFileData(outFileData); d.setFileSize(outFileData.length); if (d.getFileSize() > SafeSlingerConfig.MAX_FILEBYTES) { is.close(); showNote(String.format(getString(R.string.error_CannotSendFilesOver), SafeSlingerConfig.MAX_FILEBYTES)); refreshView(); return; } String type = URLConnection.guessContentTypeFromStream(is); if (type != null) d.setFileType(type); else { String extension = SSUtil.getFileExtensionOnly(vir.getName()); type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (type != null) { d.setFileType(type); } else { d.setFileType(SafeSlingerConfig.MIMETYPE_OPEN_ATTACH_DEF); } } } finally { is.close(); } } catch (OutOfMemoryError e) { showNote(R.string.error_OutOfMemoryError); refreshView(); return; } catch (IOException e) { showNote(e); refreshView(); return; } d.setFileName(vir.getName()); d.setFileDir(phy.getPath()); d.setMsgHash(null); }