コード例。
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
public class UploadFile
{
public static void main(String a[])
{
try
{
String auth_token = 'USER AUTH TOKEN';
String auth_scope = 'recruitapi';
String recordId = 'RECORD ID';
String file = 'FILE NAME';
File f = new File(file);
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int c;
while ((c = fis.read()) != -1)
{
bos.write(c);
}
byte[] fbArray = bos.toByteArray();
targetURL = targetURL + '?authtoken='+ auth_token +'&scope='+ auth_scope+'&version=2';
PartSource ps = new ByteArrayPartSource(file,fbArray);
PostMethod post =new PostMethod(targetURL);
Part[] fields = { new FilePart('content',ps), new StringPart('id', recordId), };
post.setRequestEntity(new MultipartRequestEntity(fields,post.getParams()));
HttpClient httpclient = new HttpClient();
httpclient.executeMethod(post);
String postResp = post.getResponseBodyAsString();
System.out.println('postResp===========> : '+postResp);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}