gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / URLUtils.java @ 2220
History | View | Annotate | Download (2.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.util; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
31 |
import java.net.MalformedURLException; |
32 |
import java.net.URL; |
33 |
|
34 |
/**
|
35 |
*
|
36 |
* @author gvSIG Team
|
37 |
*/
|
38 |
public class URLUtils { |
39 |
|
40 |
public static InputStream openStream(URL url) { |
41 |
if( url == null ) { |
42 |
return null; |
43 |
} |
44 |
try {
|
45 |
return url.openStream();
|
46 |
} catch (IOException ex) { |
47 |
// Do nthhing
|
48 |
} |
49 |
// En windows hay un problema con los URL de tipo fichero, y si estos se han
|
50 |
// construido con file.toURI().toURL() al llamar a openStream falla,
|
51 |
// asi que si falla lo volveremos a intentar abriendo el File a mano.
|
52 |
String protocol = url.getProtocol();
|
53 |
String path = url.getPath();
|
54 |
if( protocol==null || path==null || path.isEmpty() || !protocol.equalsIgnoreCase("file") ) { |
55 |
return null; |
56 |
} |
57 |
String host = url.getHost();
|
58 |
if (host==null || host.length()!=1){ |
59 |
return null; |
60 |
} |
61 |
|
62 |
File f = new File(host+":"+path); |
63 |
FileInputStream is;
|
64 |
try {
|
65 |
is = new FileInputStream(f); |
66 |
} catch (FileNotFoundException ex) { |
67 |
return null; |
68 |
} |
69 |
return is;
|
70 |
} |
71 |
|
72 |
public static void main(String[] args) throws MalformedURLException { |
73 |
File f = new File("C:\\Users\\jolic\\gvSIG\\plugins\\org.gvsig.scripting.app.mainplugin\\2.5.1\\scripts\\addons\\Arena2Reader\\i18n\\text.properties"); |
74 |
URLUtils.openStream(f.toURI().toURL()); |
75 |
} |
76 |
} |