Situation: Your partner offers a web page to upload files with a web browser.
Objective: Automate the upload so an operator is not required to upload files.
SHEEP's HttpPostClient can upload files to a web server very easily.
Free HttpPostClient download.
Here are the cmd and shell scripts to upload the content of a whole directory to a given url.
Windows Script
set HOST=http://www.compomentis.com
set SERVICE=CMSandbox/HttpPostClient01.jsp
set URL=%HOST%/%SERVICE%
for %%f in (C:\uploadSource\*.*)
do java -jar CMHttpPostClient14.jar url="%URL%" file="order=text/xml=%%f"
Unix Script
export HOST=http://www.compomentis.com
export SERVICE=CMSandbox/HttpPostClient01.jsp
export URL=$HOST/$SERVICE
for f in $(find "\uploadSource" -name '*.*');
do java -jar CMHttpPostClient14.jar url="$URL" file="order=text/xml=$f";
done
Here is a simple java program to upload anything.
imagine the following uploadsBill.txt file
order=text/xml=c:\uploadSource\order1234.xml
order=text/xml=c:\uploadSource\order1235.xml
order=text/xml=c:\uploadSource\order1236.xml
The following command (Windows or Unix)
more uploadsBill.txt | java -jar DemoHttpPostCliet.jar "http://www.compomentis.com/CMSandbox/HttpPostClient01.jsp"
Will upload each file as a text/xml order.
Here is the java source.
package com.compomentis.httppostclient;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class DemoHttpPostClient
{
// The program takes four unnamed arguments [url , user, password, output]
// Note: output is a directory to receive logfiles.
// The program then receives processing instructions from the standard input.
// You can either type them in from the keyboard or redirect a file to it.
// Processing instructions are such as: name=type=value
// For single values: The type must be left empty. Example" firstName==John
// For files: The type is an indicative content-type. Example" order=text/xml=order.xml
public static void main(String[] args)
{
try
{
// Setup the connection
HttpPostClient client = new HttpPostClient();
client.setUrl(args[0]);
client.setUserName(args[1]);
client.setPassword(args[2]);
client.setOutput(args[3]);
// To read the standard inut one text line at a time.
InputStreamReader isin = new InputStreamReader(System.in);
BufferedReader brin = new BufferedReader(isin);
// Get next processing instruction until end of file.
String nextInstruction = brin.readLine();
while(nextInstruction != null)
{
// Split the processing instruction and pass it to the client.
client.clearFields();
String parts[] = nextInstruction.split("=");
client.setField(parts[1],parts[0],parts[2]);
// Execute the upload.
client.run();.
// Report a bit, then continue with next upload.
System.out.println("server returned :" + client.getReturnedContentLength() + " bytes of " +
client.getReturnedContentType());
System.out.println("server response stored in: " + client.getOutput());
nextInstruction = brin.readLine();
}
System.out.println("Done");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|