android 本地地址轉換為url,android本地mipmap圖片轉url、絕對路徑轉URL URL URI File Path 轉換...

標簽: url uri file path

File to URI:

File file = ...;

URI uri = file.toURI();

File to URL:

File file = ...;

URL url = file.toURI().URL();

URL to File:

URL url = ...;

File file = new Path(url.getPath()).toFile();

URI to URL:

URI uri = ...;

URL url = uri.toURL();

URL to URI:

URL url = ...;

URI uri = url.toURI();

一般情況下采用上述方式都可以安全的使用.

但是, 當處理本地路徑且有空格,或者特殊字符,比如漢字等. 路徑在相互的轉換過程中, 可能會出現轉換的無效字符錯誤異常.

所以, 可以使用Eclipse提供的工具類org.eclipse.core.runtime.URIUtil (插件: org.eclipse.equinox.simpleconfigurator)來進行轉換.

URL URI File Path 轉換(原創)

比如URL to File:

URL url = ...;

File file = URIUtil.toFile(URIUtil.toURI(url));

當URL, URI直接互相轉換時,也可以使用該URIUtil工具類.

toURI

toURL

還有一個工具類,就是org.eclipse.core.runtime.FileLocator(插件: org.eclipse.equinox.common) 也可以對URL進行File的格式化. 比如toFileURL方法.

附源碼:

package org.eclipse.equinox.internal.simpleconfigurator.utils;

import java.io.File;

import java.net.*;

public class URIUtil {

private static final String SCHEME_FILE = "file"; //$NON-NLS-1$

private static final String UNC_PREFIX = "//"; //$NON-NLS-1$

public static URI append(URI base, String extension) {

try {

String path = base.getPath();

if (path == null)

return appendOpaque(base, extension);

//if the base is already a directory then resolve will just do the right thing

if (path.endsWith("/")) {//$NON-NLS-1$

URI result = base.resolve(extension);

//Fix UNC paths that are incorrectly normalized by URI#resolve (see Java bug 4723726)

String resultPath = result.getPath();

if (path.startsWith(UNC_PREFIX) && (resultPath == null || !resultPath.startsWith(UNC_PREFIX)))

result = new URI(result.getScheme(), "///" + result.getSchemeSpecificPart(), result.getFragment()); //$NON-NLS-1$

return result;

}

path = path + "/" + extension; //$NON-NLS-1$

return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path, base.getQuery(), base.getFragment());

} catch (URISyntaxException e) {

//shouldn't happen because we started from a valid URI

throw new RuntimeException(e);

}

}

private static URI appendOpaque(URI base, String extension) throws URISyntaxException {

String ssp = base.getSchemeSpecificPart();

if (ssp.endsWith("/")) //$NON-NLS-1$

ssp += extension;

else

ssp = ssp + "/" + extension; //$NON-NLS-1$

return new URI(base.getScheme(), ssp, base.getFragment());

}

public static URI fromString(String uriString) throws URISyntaxException {

int colon = uriString.indexOf(':');

int hash = uriString.lastIndexOf('#');

boolean noHash = hash < 0;

if (noHash)

hash = uriString.length();

String scheme = colon < 0 ? null : uriString.substring(0, colon);

String ssp = uriString.substring(colon + 1, hash);

String fragment = noHash ? null : uriString.substring(hash + 1);

//use java.io.File for constructing file: URIs

if (scheme != null && scheme.equals(SCHEME_FILE)) {

File file = new File(uriString.substring(5));

if (file.isAbsolute())

return file.toURI();

scheme = null;

if (File.separatorChar != '/')

ssp = ssp.replace(File.separatorChar, '/');

}

return new URI(scheme, ssp, fragment);

}

public static boolean sameURI(URI url1, URI url2) {

if (url1 == url2)

return true;

if (url1 == null || url2 == null)

return false;

if (url1.equals(url2))

return true;

if (url1.isAbsolute() != url2.isAbsolute())

return false;

// check if we have two local file references that are case variants

File file1 = toFile(url1);

return file1 == null ? false : file1.equals(toFile(url2));

}

public static File toFile(URI uri) {

try {

if (!SCHEME_FILE.equalsIgnoreCase(uri.getScheme()))

return null;

//assume all illegal characters have been properly encoded, so use URI class to unencode

return new File(uri);

} catch (IllegalArgumentException e) {

//File constructor does not support non-hierarchical URI

String path = uri.getPath();

//path is null for non-hierarchical URI such as file:c:/tmp

if (path == null)

path = uri.getSchemeSpecificPart();

return new File(path);

}

}

public static String toUnencodedString(URI uri) {

StringBuffer result = new StringBuffer();

String scheme = uri.getScheme();

if (scheme != null)

result.append(scheme).append(':');

//there is always a ssp

result.append(uri.getSchemeSpecificPart());

String fragment = uri.getFragment();

if (fragment != null)

result.append('#').append(fragment);

return result.toString();

}

public static URI toURI(URL url) throws URISyntaxException {

//URL behaves differently across platforms so for file: URLs we parse from string form

if (SCHEME_FILE.equals(url.getProtocol())) {

String pathString = url.toExternalForm().substring(5);

//ensure there is a leading slash to handle common malformed URLs such as file:c:/tmp

if (pathString.indexOf('/') != 0)

pathString = '/' + pathString;

else if (pathString.startsWith(UNC_PREFIX) && !pathString.startsWith(UNC_PREFIX, 2)) {

//URL encodes UNC path with two slashes, but URI uses four (see bug 207103)

pathString = UNC_PREFIX + pathString;

}

return new URI(SCHEME_FILE, null, pathString, null);

}

try {

return new URI(url.toExternalForm());

} catch (URISyntaxException e) {

//try multi-argument URI constructor to perform encoding

return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());

}

}

public static URL toURL(URI uri) throws MalformedURLException {

return new URL(uri.toString());

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536558.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536558.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536558.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ORACLE數據庫導出導入數據

準備工作&#xff1a; 1、登錄管理員system 2、create directory dbdata as C:\oracle\tempData;--創建備份文件夾 3、grant read,write on directory dbdata to gsjk2018;--授權讀寫為用戶 --導出(每次修改文件名)expdp gsjk2018/gsjk2018_vimtech10.0.73.32:1521/orcl direct…

linux sed名寧,Linux shell利用sed批量更改文件名的方法

微子網絡與大家分享了在Linux shell中使用sed批量更改文件名的方法。希望你看完這篇文章有所收獲。大家一起討論一下。示例去除特定字符目標&#xff1a;把2017-01-01.jpg和2018-01-01.jpg變成20170101.jpg和20180101.jpg方法&#xff1a;用空值替換全部for filein ls | grep …

android手機給iphone越獄,一臺ROOT后的安卓手機:可以用來給iOS 13越獄了

iOS 13時代的越獄工具主要包括unc0ver和Checkra1n兩款&#xff0c;前者最新的v4.2.1版本已經支持A9到A13設備從除了支持的設備和系統多&#xff0c;unc0ver的一大優勢在于可在iOS設備上獨立完成越獄操作&#xff0c;Checkra1n則需要借助電腦&#xff0c;包括重啟失效后也是如此…

502 Bad Gateway The server returned an invalid or incomplete response

問題描述&#xff1a;最近在登陸某大學網站時&#xff0c;網站如下&#xff1a; https://yzb.tju.edu.cn/ 發現登錄不進去&#xff0c;報了502 Bad Gateway The server returned an invalid or incomplete response這個錯誤。 問題解決&#xff1a;將https改為http&#xff0…

iOS VIPER架構(三)

路由是實現模塊間解耦的一個有效工具。如果要進行組件化開發&#xff0c;路由是必不可少的一部分。目前iOS上絕大部分的路由工具都是基于URL匹配的&#xff0c;優缺點都很明顯。這篇文章里將會給出一個更加原生和安全的設計&#xff0c;這個設計的特點是&#xff1a; 路由時用p…

android camera滑動,Android怎么實現小米相機底部滑動指示器

Android怎么實現小米相機底部滑動指示器發布時間&#xff1a;2021-04-15 14:39:38來源&#xff1a;億速云閱讀&#xff1a;94作者&#xff1a;小新這篇文章給大家分享的是有關Android怎么實現小米相機底部滑動指示器的內容。小編覺得挺實用的&#xff0c;因此分享給大家做個參考…

laravel安裝laravel-ide-helper擴展進行代碼提示(二)

一、擴展的地址 https://github.com/barryvdh/laravel-ide-helper二、安裝擴展 1、引入庫&#xff1a; composer require barryvdh/laravel-ide-helper composer require doctrine/dbal如果只想在開發環境上使用&#xff0c;請加上--dev composer require --dev barryvdh/larav…

android md 顏色,安卓MD(Material Design)規范

Md規范是一種設計風格&#xff0c;并不特指規范。是一種模擬紙張的手法。一、核心思想把物理世界的體驗帶進屏幕。去掉現實中的雜質和隨機性&#xff0c;保留其最原始純凈的形態、空間關系、變化與過度&#xff0c;配合虛擬世界的靈活特性&#xff0c;還原最貼近真實的體驗&…

Mariadb修改root密碼

2019獨角獸企業重金招聘Python工程師標準>>> 默認情況下&#xff0c;新安裝的 mariadb 的密碼為空&#xff0c;在shell終端直接輸入 mysql 就能登陸數據庫。 如果是剛安裝第一次使用&#xff0c;請使用 mysql_secure_installation 命令初始化。 # mysql_secure_inst…

【譯】Googler如何解決編程問題

本文是Google工程師Steve Merritt的一篇博客&#xff0c;向大家介紹他自己和身邊的同事解決編程問題的方法。 原文地址&#xff1a;blog.usejournal.com/how-a-googl… 在本文中&#xff0c;我將完整的向你介紹一種解決編程問題的策略&#xff0c;這個策略是我在日常工作中一直…

自學html和css,學習HTML和CSS的5大理由

描述人們學習HTML和CSS最常見的原因是開始從事web開發。但并不是只有web開發人員才要學習HTML和CSS的核心技術。作為一個網絡用戶&#xff0c;你需要你掌握的相關技術很多&#xff0c;但下面有5個你無法拒絕學習HTML和CSS的理由。1、輕松制作卡通動畫Web上的動畫很多年來都是使…

html 左側 樹形菜單,vue左側菜單,樹形圖遞歸實現代碼

學習vue有一段時間了&#xff0c;最近使用vue做了一套后臺管理系統&#xff0c;左側菜單需求是這樣的&#xff0c;可以多層&#xff0c;數據由后臺傳遞。也因為自己對官方文檔的不熟悉使得自己踩了不少坑&#xff0c;今天寫出來和大家一起分享。效果圖如下所示&#xff1a;先說…

Node.js的基本使用3

koa(擴展知識&#xff0c; 建議學習) koa是express超集&#xff08;進階版&#xff09;前后端分離和耦合概念介紹 面向過程 -》 面向對象 --》 面向服務數據庫 Node.js mongodb(bson json的超集) 分類&#xff1a; 關系型數據庫&#xff1a; MySql非關系型數據庫: MongoDB Mong…

Flutter的滾動以及sliver約束

Flutter框架中有很多滾動的Widget,ListView、GridView等&#xff0c;這些Widget都是使用Scrollable配合Viewport來完成滾動的。我們來分析一下這個滾動效果是怎樣實現的。 Scrollable在滾動中的作用 Scrollable繼承自StatefulWidget&#xff0c;我們看一下他的State的build方法…

頁面增加html,為靜態頁面HTML增加session功能

一般來說&#xff0c;只有服務器端的CGI程序(ASP、PHP、JSP)具有session會話功能&#xff0c;用來保存用戶在網站期間(會話)的活動數據信息&#xff0c;而對于數量眾多的靜態頁面(HTML)來說&#xff0c;只能使用客戶端的cookies來保存臨時活動數據&#xff0c;但對于cookies的操…

關于Istio 1.1,你所不知道的細節

本文整理自Istio社區成員Star在 Cloud Native Days China 2019 北京站的現場分享 第1則 主角 Istio Istio作為service mesh領域的明星項目&#xff0c;從2016年發布到現在熱度不斷攀升。 Istio & Envoy Github Star Growth 官網中Istio1.1的架構圖除了數據面的Envoy和控制面…

html調用父頁面的函數,js調用父框架函數與彈窗調用父頁面函數的方法

調用父級中的 aaa的函數子頁面中:οnclick"window.parent.frames.aaa()"父頁面中:function aaa(){alert(‘bbbbb’);}----------------------------------------------frame框架里的頁面要改其他同框架下的頁面或父框架的頁面就用parentwindow.opener引用的是window.…

讀卡距離和信號強度兩方面來考慮

選擇物聯宇手持終端機的時候&#xff0c;你可以參考以下幾個原則&#xff1a;選擇行業需要應用功能&#xff0c;能有效控制好預算。屏幕界面需要高清晰的&#xff0c;選用分辨率較高的能更好的支持展現。按照項目所需求的來分析&#xff0c;需要從讀卡距離和信號強度兩方面來考…

html script 放置位置,script標簽應該放在HTML哪里,總結分享

幾年前&#xff0c;有經驗的程序員總是讓我們將很明顯&#xff0c;現在瀏覽器有了更加酷的兼容方式&#xff0c;這篇文章&#xff0c;俺將跟大家一起來學習script標簽的async和defer新特性&#xff0c;探討script應該放在哪里更好。頁面加載方式在我們討論當瀏覽器加載帶有獲取…

2021吉林高考26日幾點可以查詢成績,2021吉林高考成績查分時間及入口

2021吉林高考成績查分時間及入口2021吉林高考成績查分時間及入口&#xff0c;有一些高考生真的很積極&#xff0c;考完試當天就將答案給對好了&#xff0c;考試嘛&#xff0c;站在旁觀者的角度來看總是有人歡喜有人憂。估出來分數不咋地的&#xff0c;整個六月就毀了。2021吉林…