日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 本地地址转换为url,android本地mipmap图片转url、绝对路径转URL URL URI File Path 转换...

發(fā)布時(shí)間:2025/3/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 本地地址转换为url,android本地mipmap图片转url、绝对路径转URL URL URI File Path 转换... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

標(biāo)簽: 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();

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

但是, 當(dāng)處理本地路徑且有空格,或者特殊字符,比如漢字等. 路徑在相互的轉(zhuǎn)換過(guò)程中, 可能會(huì)出現(xiàn)轉(zhuǎn)換的無(wú)效字符錯(cuò)誤異常.

所以, 可以使用Eclipse提供的工具類(lèi)org.eclipse.core.runtime.URIUtil (插件: org.eclipse.equinox.simpleconfigurator)來(lái)進(jìn)行轉(zhuǎn)換.

URL URI File Path 轉(zhuǎn)換(原創(chuàng))

比如URL to File:

URL url = ...;

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

當(dāng)URL, URI直接互相轉(zhuǎn)換時(shí),也可以使用該URIUtil工具類(lèi).

toURI

toURL

還有一個(gè)工具類(lèi),就是org.eclipse.core.runtime.FileLocator(插件: org.eclipse.equinox.common) 也可以對(duì)URL進(jìn)行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());

}

}

總結(jié)

以上是生活随笔為你收集整理的android 本地地址转换为url,android本地mipmap图片转url、绝对路径转URL URL URI File Path 转换...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美色图19p | 国产麻豆精品在线观看 | 精品久久久久久久 | 精品成在人线av无码免费看 | 欧美成人a∨高清免费观看 国产精品999视频 | 免费一区二区三区 | 亚洲综合激情小说 | 欧美激情va永久在线播放 | 午夜激情小视频 | 91麻豆免费视频 | 91们嫩草伦理 | 凹凸精品熟女在线观看 | 欧美极品少妇xxxxⅹ裸体艺术 | 亚欧洲乱码视频 | 欧美激情一级 | 男人和女人做爽爽视频 | 久久老熟女一区二区三区 | av一起看香蕉| 亚洲欧美一区二区三区久久 | 男人和女人日批 | 在线看日韩av | 人妻洗澡被强公日日澡 | 强行挺进皇后紧窄湿润小说 | 日韩av免费在线播放 | 日本极品喷水 | 黄色理论视频 | 伊人久久免费视频 | 国产日产亚洲系列最新 | 精品国产一区二区三区性色av | 日日夜夜噜 | 精品一区二区三区在线播放 | 91干干| 国产一区二区高清视频 | 呦呦精品| 欧美成人亚洲 | 久草青青视频 | 在线免费观看av的网站 | 精品视频一二三 | 伊人成人在线 | 特级淫片裸体免费看 | 午夜爽视频 | 先锋影音一区二区 | 亚洲综合激情网 | wwwxx日本| 综合久| 欧美色香蕉 | 九九看片 | 中文字幕自拍偷拍 | aa一级黄色片 | 天堂网一区二区 | 国产亚洲精品女人久久久久久 | 欧美 在线 | 青青偷拍视频 | 色爱五月天 | 国产精九九网站漫画 | 亚洲国产成人一区二区精品区 | 国产剧情av在线 | 玖玖色资源 | 国产亚洲精品成人av在线 | 免费视频福利 | 欧美激情在线播放 | 欧美丰满熟妇bbb久久久 | 国产又爽又黄的视频 | 外国一级片| 女婴高潮h啪啪 | 久久久高清视频 | 日韩亚洲欧美在线观看 | 黑料视频在线观看 | 老熟妇毛片 | 欧美黄色免费在线观看 | 亚洲天堂网在线观看 | 九九av在线 | 久久黄色精品视频 | 男人午夜免费视频 | 国产色在线视频 | 日本福利在线观看 | 国产区福利 | 伊人网五月天 | 国产无套粉嫩白浆内谢 | 国产探花精品在线 | 国产精品999在线观看 | 日本色中色 | 国产精品.com | 午夜少妇福利 | 色阁av | 欧性猛交ⅹxxx乱大交 | 综合久久五月天 | 懂色中文一区二区在线播放 | 毛片网站在线免费观看 | 一区二区三区日韩精品 | 亚洲久久在线 | www.com黄色片 | 国产精品久久久久久99 | 天天爱天天做天天爽 | 国产精品偷伦视频免费看 | 国产美女精品在线 | 91网站视频在线观看 | 肥臀浪妇太爽了快点再快点 | 影音先锋欧美在线 |