Java Reference
In-Depth Information
import com.heatonresearch.httprecipes.html.FormUtility;
public class FormUpload
{
/**
* Upload a file.
* @param uid The user id for the form.
* @param pwd The password for the form.
* @param file The file to upload.
* @throws IOException Thrown if any I/O error occurs.
*/
public void upload(String uid, String pwd, File file)
throws IOException
{
// get the boundary used for the multipart upload
String boundary = FormUtility.getBoundary();
URLConnection http =
(new URL("http://www.httprecipes.com/1/7/uploader.php"))
.openConnection();
http.setDoOutput(true);
// specify that we will use a multipart form
http.setRequestProperty(
"Content-Type", "multipart/form-data; boundary="
+ boundary);
OutputStream os = http.getOutputStream();
// construct a form
FormUtility form = new FormUtility(os, boundary);
form.add("uid", uid);
form.add("pwd", pwd);
form.add("uploadedfile", file);
form.complete();
// perform the upload
InputStream is = http.getInputStream();
is.close();
}
/**
* The main method attempts to obtain the user id, password
* and local filename for the upload.
* @param args The user id, password and local filename.
*/
public static void main(String args[])
{
try
{
Search WWH ::




Custom Search