Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.multipart.FilePart;
- import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
- import org.apache.commons.httpclient.methods.multipart.Part;
- import org.apache.commons.httpclient.methods.multipart.StringPart;
- public class RtCommentSample {
- public static void main(String[] args) {
- String targetURL = args[0]+"/REST/1.0/ticket/"+args[1]+"/comment?user=root&pass=password";
- PostMethod filePost = new PostMethod(targetURL);
- try {
- String content = "id: "+args[1]+"\nAction: comment\nText: "+args[2];
- Part[] parts = null;
- if(args.length > 3){
- parts = new Part[2];
- File targetFile = new File(args[3]);
- parts[0] = new FilePart("attachment_1",targetFile.getName(), targetFile);
- content += "\nAttachment: "+targetFile.getName();
- }else{
- parts = new Part[1];
- }
- System.out.println(content);
- parts[parts.length -1] = new StringPart("content",content);
- filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
- HttpClient client = new HttpClient();
- client.getHttpConnectionManager(). getParams().setConnectionTimeout(5000);
- int status = client.executeMethod(filePost);
- if (status == HttpStatus.SC_OK) {
- System.out.println("Upload complete, response=" + filePost.getResponseBodyAsString());
- } else {
- System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- filePost.releaseConnection();
- }
- }
- }
Add Comment
Please, Sign In to add comment