スキップしてメイン コンテンツに移動

投稿

ラベル(uri)が付いた投稿を表示しています

URI Encode for Java

I always forget and struggling about the behavior of Java methods and classes for encoding uri. To clear my heads I will post small test codes. import java.net.URI; import java.net.URLEncoder; import java.util.BitSet; import org.apache.commons.httpclient.util.URIUtil; public class URIEncodeExample { // hehe using Japanese :p private static final String INPUT = "あいうえお/"; public static void main(String[] args) throws Exception { // java.net.URI // slash is not encoded because it is detected as a path separator // %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/ System.out.println(URI.create(INPUT).toASCIIString()); // java.net.URI + relativize // slash is not encoded because it is detected as a path separator // %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/ URI root = new URI("http", "dummy.com", null, null); URI uri = new URI("http", "dummy.com",

C#: ファイルパスから絶対URIを返すコード

ファイルパスから絶対URIを返すコードです。(特に相対ファイルパスで便利かも知れません。) public static string GetAbsoluteUri(string filepath){ FileInfo fileInfo = new FileInfo(filePath); Uri uri = new Uri(fileInfo.FullName); return uri.AbsoluteUri; } ちなみにコードの色づけには google-code-prettify を使わせて頂きました。さすがGoogle! 使い方は以下のリンクが参考になります。 Learning Log Book: Bloggerでシンタックス・ハイライト