Created
September 17, 2011 19:31
-
-
Save rotty3000/1224272 to your computer and use it in GitHub Desktop.
simple sample client for fileupload to dl, requires commons-httpclient and it's dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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