1 package tim.prune.function.gpsies;
\r
3 import java.net.HttpURLConnection;
\r
4 import java.net.URLConnection;
\r
6 import java.io.IOException;
\r
7 import java.io.InputStream;
\r
8 import java.util.Random;
\r
9 import java.io.OutputStream;
\r
12 * Taken from the Client HTTP Request class at com.myjavatools.web
\r
13 * and subsequently simplified and modified
\r
14 * @author Vlad Patryshev
\r
16 public class FormPoster
\r
18 private URLConnection _connection = null;
\r
19 private OutputStream _os = null;
\r
20 private static final Random RANDOM_GEN = new Random();
\r
21 private static final String BOUNDARY = "---------------------------"
\r
22 + randomString() + randomString() + randomString();
\r
25 /** Connect (if not already connected) */
\r
26 protected void connect() throws IOException {
\r
27 if (_os == null) _os = _connection.getOutputStream();
\r
30 /** Write a single character */
\r
31 protected void write(char c) throws IOException {
\r
36 /** Write a string */
\r
37 protected void write(String s) throws IOException {
\r
39 _os.write(s.getBytes());
\r
42 /** Write a -r-n newline sequence */
\r
43 protected void newline() throws IOException {
\r
47 /** Write a string followed by a newline */
\r
48 protected void writeln(String s) throws IOException {
\r
53 /** Generate a random alphanumeric string */
\r
54 private static String randomString() {
\r
55 return Long.toString(RANDOM_GEN.nextLong(), 36);
\r
58 /** Write a boundary marker */
\r
59 private void boundary() throws IOException {
\r
66 * Creates a new multipart POST HTTP request for a specified URL
\r
67 * @param url the URL to send request to
\r
68 * @throws IOException
\r
70 public FormPoster(URL inUrl) throws IOException
\r
72 _connection = inUrl.openConnection();
\r
73 _connection.setDoOutput(true);
\r
74 _connection.setRequestProperty("Content-Type",
\r
75 "multipart/form-data; boundary=" + BOUNDARY);
\r
78 /** Write a header with the given name */
\r
79 private void writeName(String inName) throws IOException
\r
82 write("Content-Disposition: form-data; name=\"");
\r
88 * adds a string parameter to the request
\r
89 * @param name parameter name
\r
90 * @param value parameter value
\r
91 * @throws IOException
\r
93 public void setParameter(String inName, String inValue) throws IOException
\r
97 newline(); newline();
\r
101 /** Pipe the contents of the input stream to the output stream */
\r
102 private static void pipe(InputStream in, OutputStream out) throws IOException
\r
104 byte[] buf = new byte[500000];
\r
106 synchronized (in) {
\r
107 while((nread = in.read(buf, 0, buf.length)) >= 0) {
\r
108 out.write(buf, 0, nread);
\r
116 * adds a file parameter to the request
\r
117 * @param inName parameter name
\r
118 * @param inFilename the name of the file
\r
119 * @param inStream input stream to read the contents of the file from
\r
120 * @throws IOException
\r
122 public void setParameter(String inName, String inFilename, InputStream inStream) throws IOException
\r
126 write("; filename=\"");
\r
130 write("Content-Type: ");
\r
131 String type = URLConnection.guessContentTypeFromName(inFilename);
\r
132 if (type == null) {type = "application/octet-stream";}
\r
135 pipe(inStream, _os);
\r
140 * posts the requests to the server
\r
141 * @return input stream with the server response
\r
142 * @throws IOException
\r
144 public InputStream post() throws IOException
\r
149 return _connection.getInputStream();
\r
153 * @return the HTTP response code, 200 for success or -1 if not available
\r
155 public int getResponseCode() throws IOException
\r
157 if (_connection != null && _connection instanceof HttpURLConnection) {
\r
158 return ((HttpURLConnection) _connection).getResponseCode();
\r