MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / openUrlAsString

Method openUrlAsString

ij/src/main/java/ij/IJ.java:2121–2148  ·  view source on GitHub ↗

Opens a URL and returns the contents as a string. Returns " " if there an error, including host or file not found.

(String url)

Source from the content-addressed store, hash-verified

2119 Returns "<Error: message>" if there an error, including
2120 host or file not found. */
2121 public static String openUrlAsString(String url) {
2122 //if (!trustManagerCreated && url.contains("nih.gov")) trustAllCerts();
2123 url = Opener.updateUrl(url);
2124 if (debugMode) log("OpenUrlAsString: "+url);
2125 StringBuffer sb = null;
2126 url = url.replaceAll(" ", "%20");
2127 try {
2128 //if (url.contains("nih.gov")) addRootCA();
2129 URL u = new URL(url);
2130 URLConnection uc = u.openConnection();
2131 long len = uc.getContentLength();
2132 if (len>5242880L)
2133 return "<Error: file is larger than 5MB>";
2134 InputStream in = u.openStream();
2135 BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
2136 sb = new StringBuffer() ;
2137 String line;
2138 while ((line=br.readLine()) != null)
2139 sb.append (line + "\n");
2140 in.close ();
2141 } catch (Exception e) {
2142 return("<Error: "+e+">");
2143 }
2144 if (sb!=null)
2145 return new String(sb);
2146 else
2147 return "";
2148 }
2149
2150 /** Saves the current image, lookup table, selection or text window to the specified file path.
2151 The path must end in ".tif", ".jpg", ".gif", ".zip", ".raw", ".avi", ".bmp", ".fits", ".pgm", ".png", ".lut", ".roi" or ".txt". */

Callers 5

doFileMethod · 0.95
openUrlMethod · 0.95
doIncludeMethod · 0.95
openExampleMethod · 0.95
openTextURLMethod · 0.95

Calls 6

updateUrlMethod · 0.95
logMethod · 0.95
replaceAllMethod · 0.80
appendMethod · 0.65
closeMethod · 0.65
readLineMethod · 0.45

Tested by

no test coverage detected