public
Created

simple sample client for fileupload to dl, requires commons-httpclient and it's dependencies.

  • Download Gist
FileUploadSample.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
import java.io.File;
import java.io.IOException;
 
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
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;
import org.apache.commons.httpclient.params.HttpMethodParams;
 
public class FileUploadTest {
 
public static void main(String[] args) throws Exception {
if (args.length != 6) {
System.out.println(
"\nUsage: java -classpath " +
"./:commons-httpclient.jar:commons-logging.jar:commons-codec.jar " +
"FileUploadTest <host> <port> <login> <password> " +
"<groupId> <file>");
 
return;
}
 
FileUploadTest fue = new FileUploadTest();
 
String host = args[0];
int port = Integer.parseInt(args[1]);
String login = args[2];
String password = args[3];
long groupId = Long.parseLong(args[4]);
File file = new File(args[5]);
 
if (file.exists() && !file.isDirectory()) {
fue.update(host, port, login, password, groupId, file);
}
}
 
public void update(
String host, int port, String login, String password, long groupId,
File file)
throws HttpException, IOException {
 
HttpClient client = new HttpClient();
 
client.getState().setCredentials(
new AuthScope(host, 8080, "PortalRealm"),
new UsernamePasswordCredentials(login, password));
 
PostMethod method = new PostMethod(
"http://" + host +
((port > 0) && (port < 65535) ? ":" + port : "") +
"/tunnel-web/secure/jsonws/dlapp/add-file-entry");
 
method.getParams().setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
 
Part[] parts = new Part[] {
new StringPart("repositoryId", String.valueOf(groupId)),
new StringPart("folderId", "0"),
new StringPart("sourceFileName", file.getName()),
new StringPart("mimeType", ""),
new StringPart("title", file.getName()),
new StringPart("description", ""),
new StringPart("changeLog", ""),
new FilePart("file", file.getName(), file)
};
 
method.setRequestEntity(
new MultipartRequestEntity(parts, method.getParams())
);
 
try {
method.setDoAuthentication(true);
 
int statusCode = client.executeMethod(method);
 
String responseBody = new String(method.getResponseBody());
 
if (statusCode != HttpStatus.SC_OK) {
throw new HttpException(method.getStatusLine().toString());
}
else {
System.out.println(responseBody);
}
}
finally {
method.releaseConnection();
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.