Time to blog my multipart file upload version 2. In this version I am no longer using the MultiPartFormOutputStream.java (see my previous blog: http://lindaocta.com/?p=185)
In this version, I use java dependencies:
- httpclient: download HttpClient and it’s dependencies from: http://hc.apache.org/httpclient-3.x/tutorial.html
- io: download from: http://commons.apache.org/downloads/download_io.cgi
Transformer_two.java code:
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
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.io.IOUtils;
class transformer_two{
public static void main(String [] args) {
String convertUrl = "http://ice-service.usq.edu.au/api/convert/";
if(args.length == 1) {
String fileName = args[0];
System.out.println(fileName);
File f = new File(fileName);
if(f.exists()) {
String extResult = getExtension(f);
if (!extResult.startsWith("Error")) {
convertUrl = convertUrl + extResult;
getRendition(f, convertUrl);
}
} else {
System.out.println("Error: file doesn't exist!");
}
} else {
System.out.println("Usage: java transformer fileName");
}
}
public static void getRendition (File sourceFile, String convertUrl) {
try{
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod(convertUrl);
Part[] parts = {
new StringPart("zip", "1"),
new StringPart("toc", "1"),
new StringPart("pdfLink", "1"),
new StringPart("pathext", ""),
new StringPart("template", getTemplate()),
new FilePart("file", sourceFile)
};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
client.executeMethod(filePost);
//Get Response
InputStream is = filePost.getResponseBodyAsStream();
String[] filePart = sourceFile.getName().split("\\.");
System.out.println(sourceFile.getName());
String outputFilename = "/tmp/"+ filePart[0] + ".zip";
FileOutputStream fos = new FileOutputStream(outputFilename);
IOUtils.copy(is, fos);
is.close();
fos.close();
System.out.println("Rendition zip file is located at: " + outputFilename);
} catch (Exception e) {
System.out.println("An error occurred: " + e.getClass());
e.printStackTrace();
}
}
private static String getExtension(File fileObject) {
/** getExtension method
* @param File fileObject
* @return String
*/
String[] parts = fileObject.getName().split("\\.");
String ext = "";
if (parts.length == 2) {
ext = parts[1];
return ext;
}
return "Error: Unable to detect file extension properly!";
}
private static String getTemplate (){
String template = "<html>" +
"<head>" +
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>" +
"<title>Default Template</title>" +
"<style type='text/css'>" +
".rendition-links { text-align: right; }" +
".body table td { vertical-align: top; }" +
"</style>" +
"<style class='sub style-css' type='text/css'></style>" +
"</head>" +
"<body>" +
"<div class='rendition-links'>" +
"<span class='ins source-link'></span>" +
"<span class='ins slide-link'></span>" +
"<span class='ins pdf-rendition-link'></span>" +
"</div>" +
"<h1 class='ins title'></h1>" +
"<div class='ins page-toc'></div>" +
"<div class='ins body'></div>" +
"</body>" +
"</html>";
return template;
}
}
To run the new transformer_two.java:
- download the code from: transformer_two.java and save as transformer_two.java
- create “lib” folder in the same level
- Put all the jar file from the downloaded dependencies above in “lib” folder
- Compile transformer_two.java:
javac -d . -classpath ./lib/commons-httpclient-3.1.jar:./lib/commons-io-1.4.jar transformer_two.java
- To run transformer_two:
java -classpath .:./lib/commons-httpclient-3.1.jar:./lib/commons-io-1.4.jar:./lib/commons-logging-1.1.1.jar:./lib/commons-codec-1.3.jar transformer_two ABSOLUTE_PATH_TO_YOUR_DOCUMENT
1 comment
software developer says:
August 17, 2009 at 10:48 am (UTC -4)
Nice post,
Thanks I used it and I loved it thanks
Thanks for writing about it