I have an obscure issue. I am in the process of porting some Perl to Java and one of the methods in the Perl code posts to a jsp app and downloads a zip file. The working part of the Perl code is as follows which appears to be using a get to retrieve the file.
$mech->get ( $url );
$mech->submit_form(
fields => {
upload => variable1,
selectValue => variable2,
},
);
The jsp page is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Extract Features</title>
</head>
<body>
<form action="extract.zip" method="post" enctype="multipart/form-data">
<table>
<tr>
<th align="left">File:</th>
<td><input type="file" name="upload"></td>
</tr>
<tr>
<th align="left">Code:</th>
<td><select name="selectValue">
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
And the Java code I am using to try to access it:
URL url = new URL(s.getUrlScheme(), s.getUrlHost(), s.getUrlFile());
String urlParameters = "upload=C:\\testFile.txt&selectValue=14";
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("charset", "ISO-8859-1");
connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.length()));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(".\\test.html");
int data;
while ((data=is.read()) != -1) {
os.write(data);
}
is.close();
os.close();
connection.disconnect();
First of all sorry for the example I have posted in the comment. I thought you were trying to perform a simple
POST
request by just submitting some values. I have missed that you were also uploading a file. So since you are also uploading a file (note that you don't just pass a filename here) this is a mutlipart request (you see that in your jsp:enctype="multipart/form-data"
) and it has a little more work to do that programmatically.When you are submitting a multipart form the request looks like this:
That strange alphanumerics are called a boundary (are chosen randomly by the browser) and are used to distringuish between submitted fields (files or normal fields). In the above example the submitted fields were:
<input type="file">
in the form. The user selected the file namedtest.bin
. The file data was a single text lineTest data
"Submit"
of a<input type="submit">
buttonBelow I have copied the code from an example at code.java.net that provides a class that can help you in creating such requests.
Please modify any hard-coded urls and files to match your case. I have not tried it out but it should give you the idea of how to create a request like the above one. (Note that there are two classes: One that constructs the request and one with a
main()
method to test it)Side note: The change of line character
\r\n
does not depend on the system but it is a part of how multipart requests are constructed. So you should use it as is no matter if your system is Windows or LinuxI hope that helps