For testing I needed to simulate a web-browser doing a file-upload. I tried to Google this but I couldn't find a suitable answer that worked. So I rolled my own. This might save other people some trouble too. First comes the MIMEMultipart object, which stores the body of a multipart post:
import java.io.FileInputStream;
import java.io.File;
public class MIMEMultipart
{
StringBuilder text;
static String CRLF = "\r\n";
String boundary;
public MIMEMultipart()
{
text = new StringBuilder();
boundary = Long.toHexString(
System.currentTimeMillis());
}
public String getContent()
{
return text.toString();
}
public String getBoundary()
{
return boundary;
}
public int getLength()
{
return text.length();
}
public void putStandardParam( String name,
String value, String encoding )
{
StringBuilder sb = new StringBuilder();
sb.append("--" + boundary).append(CRLF);
sb.append("Content-Disposition: form-data; "
+"name=\""+name+"\"");
sb.append(CRLF);
sb.append("Content-Type: text/plain; charset="
+ encoding );
sb.append(CRLF);
sb.append(CRLF);
sb.append(value);
sb.append(CRLF);
text.append( sb.toString() );
}
public void putBinaryFileParam( String name,
String fileName, String mimeType,
String encoding ) throws Exception
{
// compose the header
StringBuilder sb = new StringBuilder();
sb.append( "--"+boundary );
sb.append( CRLF );
sb.append("content-disposition: form-data; "
+"name=\"" );
sb.append( name );
sb.append( "\"; filename=\"");
sb.append( fileName );
sb.append( "\"" );
sb.append( CRLF );
sb.append("Content-Type: "+mimeType );
sb.append( CRLF );
sb.append("Content-Transfer-Encoding: binary");
sb.append( CRLF ); // need two of these
sb.append( CRLF );
text.append( sb.toString() );
// now for the file
File input = new File( fileName );
FileInputStream fis = new FileInputStream(input);
byte[] data = new byte[(int)input.length()];
fis.read( data );
fis.close();
text.append( new String(data,encoding) );
text.append( CRLF );
}
public void finish()
{
text.append( "--" );
text.append( boundary );
text.append( "--" );
text.append( CRLF );
}
}
To call it, open a standard Java URLConnection:
private static void printResponse(
URLConnection conn )
{
try
{
InputStream is = conn.getInputStream();
while ( is.available() != 0 )
{
byte[] data = new byte[is.available()];
is.read( data );
System.out.println(new String(data,
"UTF-8"));
}
}
catch ( Exception e )
{
e.printStackTrace( System.out );
}
}....
URL url2 = new URL("http://localhost:8080/strip");
URLConnection conn = url2.openConnection();
MIMEMultipart mmp = new MIMEMultipart();
mmp.putStandardParam( Params.FORMAT,Formats.STIL,
"UTF-8" );
mmp.putStandardParam( Params.STYLE,"TEI/drama",
"UTF-8" );
mmp.putBinaryFileParam( Params.RECIPE,"recipe.xml",
"application/xml","UTF-8" );
mmp.putBinaryFileParam( Params.XML,
"act1-scene2-F1.xml",
"application/xml","UTF-8" );
mmp.finish();
conn.setDoOutput(true);
conn.setUseCaches(false);
((HttpURLConnection)conn).setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Content-Type",
"multipart/form-data, boundary="
+mmp.getBoundary());
conn.setRequestProperty("Content-Length",
Integer.toString(mmp.getLength()));
OutputStream output = conn.getOutputStream();
output.write( mmp.getContent().getBytes() );
output.flush();
output.close();
// get response and print it
printResponse( conn );
Extend MIMEMultipart if you like by adding a method for plain text files and other types of part.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.