Switch to full style
General Java code examples
Post a reply

return file path after encoded using UTF-8 from URL

Mon Apr 22, 2013 10:57 pm

Convert The given URL To String file path
java code
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLDecoder;


public class URLUtility {


public static String getFileFromURL(URL urlToDecode) {


String urlDecodedFilePath = urlToDecode.getFile();

String fixedFileString = null;
try { // Try to use 1.4 method with the recommended
//charset UTF-8

Class decoderClass = URLDecoder.class;
Method decodeMethod = decoderClass.getMethod("decode", new Class[] {
String.class , String.class });

Object fixedFileObject = decodeMethod.invoke(null, new Object[] {
urlDecodedFilePath, "UTF-8" });

fixedFileString = (fixedFileObject == null) ? null
: fixedFileObject.toString();
} catch (Exception e) { // Pre-1.4 version, no need to
//decode it.
fixedFileString = urlDecodedFilePath;
}

return (fixedFileString);
}

}




Post a reply
  Related Posts  to : return file path after encoded using UTF-8 from URL
 read() return when it has reached the end of a file     -  
 get include path     -  
 get Folder path and size     -  
 Dijkstra ( Shortage Path )     -  
 Dijkstra ( Shortest Path )     -  
 draw General Path     -  
 shortest path algorithm java     -  
 readLine() return value     -  
 Function return more than one value     -  
 Encrypt/Decrypt a file from source file to target file.     -  

Topic Tags

Java Files and I/O