Skip to content

@rotty3000 /FileUploadSample.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
simple sample client for fileupload to dl, requires commons-httpclient and it's dependencies.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.