Revision 2219
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/URLUtils.java | ||
---|---|---|
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 |
File f = new File(path); |
|
58 |
FileInputStream is; |
|
59 |
try { |
|
60 |
is = new FileInputStream(f); |
|
61 |
} catch (FileNotFoundException ex) { |
|
62 |
return null; |
|
63 |
} |
|
64 |
return is; |
|
65 |
} |
|
66 |
|
|
67 |
public static void main(String[] args) throws MalformedURLException { |
|
68 |
File f = new File("/tmp/test.txt"); |
|
69 |
URLUtils.openStream(f.toURI().toURL()); |
|
70 |
} |
|
71 |
} |
Also available in: Unified diff