Java Reference
In-Depth Information
How to do it...
The abbreviated code given next shows you the major components needed to achieve
binary data upload to a remote web server. Refer to ch06/source-code/src/http/
HttpRequestFileUpload.fx for a complete code listing.
1. The first portion of the code defines class FormPart used to encapsulate the binary
data and the required HTTP multi-part boundary markers that are used to decorate
the data so the server can parse it.
def MARKER = "--";
def CRLF = "\r\n";
def BOUNDARY = "7d226f700d0";
def CONTENT_TYPE = "multipart/form-data; boundary={BOUNDARY}";
class FormPart {
public-init var name:String;
public var value:String;
public var file:File;
var conv = URLConverter{}
...
public function writeFileTo ( out :OutputStream):Void {
if(file != null){
var header = new java.lang.StringBuilder();
header
.append(CRLF)
.append(MARKER)
.append(BOUNDARY)
.append(CRLF)
.append("Content-Disposition: form-data;
name=\"{name}\";
filename=\"{file.getAbsolutePath()}\"")
.append(CRLF)
.append("Content-Type: application/octet-stream")
.append(CRLF)
.append(CRLF);
// write header to output stream
out. write (header.toString().getBytes());
// copy file content to out stream
var byteRead:Integer;
var input = new FileInputStream (file);
try{
while ((byteRead = input.read()) != -1){
out .write( byteRead );
}
}finally{
input.close();
 
Search WWH ::




Custom Search