Rev 455 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
455 | jmachado | 1 | package pt.estgp.estgweb.utils; |
2 | |||
3 | import jomm.utils.MimeTypeGuesser; |
||
4 | |||
5 | import java.io.*; |
||
6 | import java.net.ProtocolException; |
||
7 | import java.net.URL; |
||
8 | import java.net.URLConnection; |
||
9 | import java.net.HttpURLConnection; |
||
10 | |||
11 | /** |
||
12 | * @author Jorge Machado |
||
13 | * @date 13/Nov/2009 |
||
14 | * @time 17:24:38 |
||
15 | * @email machadofisher@gmail.com |
||
16 | */ |
||
17 | public class HttpClient |
||
18 | { |
||
19 | public static 5+0%2Fdocs%2Fapi+InputStream">InputStream sendFile(1.5.0/docs/api/java/lang/String.html">String url, 1.5.0/docs/api/java/io/File.html">File file) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/java/net/ProtocolException.html">ProtocolException { |
||
20 | |||
21 | 1.5.0/docs/api/java/lang/String.html">String mimeType = MimeTypeGuesser.getInstance().guessMimeType(file); |
||
22 | |||
23 | 1.5.0/docs/api/java/net/URL.html">URL urlSolr = new 1.5.0/docs/api/java/net/URL.html">URL(url); |
||
24 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection urlConnection = urlSolr.openConnection(); |
||
25 | urlConnection.addRequestProperty("Content-type",mimeType); |
||
26 | |||
27 | ((1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) urlConnection).setRequestMethod("GET"); |
||
28 | |||
29 | |||
30 | urlConnection.setDoInput(true); |
||
31 | urlConnection.setDoOutput(true); |
||
32 | urlConnection.setUseCaches(false); |
||
33 | 5+0%2Fdocs%2Fapi+OutputStream">OutputStream outputStream = new 5+0%2Fdocs%2Fapi+DataOutputStream">DataOutputStream(urlConnection.getOutputStream()); |
||
34 | |||
35 | 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream fStream = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file); |
||
36 | byte[] bytes = new byte[1024]; |
||
37 | int read; |
||
38 | while((read = fStream.read(bytes)) >= 0) |
||
39 | { |
||
40 | outputStream.write(bytes,0,read); |
||
41 | } |
||
42 | outputStream.flush(); |
||
43 | outputStream.close(); |
||
44 | |||
45 | return new 5+0%2Fdocs%2Fapi+DataInputStream">DataInputStream(urlConnection.getInputStream()); |
||
46 | } |
||
47 | |||
48 | public static 5+0%2Fdocs%2Fapi+InputStream">InputStream sendXmlData(1.5.0/docs/api/java/lang/String.html">String url, 1.5.0/docs/api/java/lang/String.html">String xmlData) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/java/net/ProtocolException.html">ProtocolException { |
||
49 | |||
50 | 1.5.0/docs/api/java/net/URL.html">URL urlSolr = new 1.5.0/docs/api/java/net/URL.html">URL(url); |
||
51 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection urlConnection = urlSolr.openConnection(); |
||
52 | urlConnection.addRequestProperty("Content-type","text/xml"); |
||
53 | |||
54 | ((1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) urlConnection).setRequestMethod("GET"); |
||
55 | |||
56 | |||
57 | urlConnection.setDoInput(true); |
||
58 | urlConnection.setDoOutput(true); |
||
59 | urlConnection.setUseCaches(false); |
||
60 | 5+0%2Fdocs%2Fapi+OutputStream">OutputStream outputStream = new 5+0%2Fdocs%2Fapi+DataOutputStream">DataOutputStream(urlConnection.getOutputStream()); |
||
61 | |||
62 | outputStream.write(xmlData.getBytes()); |
||
63 | |||
64 | outputStream.flush(); |
||
65 | outputStream.close(); |
||
66 | |||
67 | return new 5+0%2Fdocs%2Fapi+DataInputStream">DataInputStream(urlConnection.getInputStream()); |
||
68 | } |
||
69 | |||
70 | } |