Java Reference
In-Depth Information
}
}
}
}
2. The second part of the code utilizes HttpRequest in conjunction with the
FormPart class to send the multi-part form to the the server.
def url = "http://localhost:8080/webapp/upload/save";
def file = new File("image.png");
def fileLen = file.length();
// HttpRequest declaration which uses class FormData
var http:HttpRequest = HttpRequest {
location: url
method: HttpRequest.POST
headers : [
HttpHeader
{name:HttpHeader.CONTENT_TYPE value:CONTENT_TYPE},
]
onOutput :function(out: OutputStream){
try {
// write parts
def filePart = FormPart {name:"file" file:file}
filePart.writeFileTo (out);
// close multipart with a footer
var footer = new java.lang.StringBuilder();
footer
.append(CRLF)
.append(MARKER)
.append(BOUNDARY)
.append(MARKER);
out.write (footer.toString().getBytes());
}finally{
out.close();
}
}
...
}
http.start();
When this code is executed, it will invoke HttpRequest.start() , which will cause the
content of the file image.png to be uploaded to the server.
 
Search WWH ::




Custom Search