Hi,
I got an applet with piece of code, to upload a file from client machine to
server box.
Below is the chunck of it....which uses java.net.URLConnection.
Can u plz. give the conversion code / steps using HTTPClient?????
Basically applet upload the file by verifying the MD5-Hash code of the file.
-----------------------------------------------------------------------------------------------------------------------------------------------
// Applet piece of code - chunck
//fileName = path to the file that is to be uploaded.
File inFile = new File(fileName);
long totLen = inFile.length(); //SR#464337DG
long trnsLen=0; //SR#464337DG
InputStream in = new BufferedInputStream(new FileInputStream(inFile));
byte[] hash = calculateHash(inFile);
//Contact the InitUploadServlet to verify if partial upload exists
URL u = getCodeBase();
URL dest = new URL("https://abc.com/ABCProject/servlet/InitUploadServlet");
java.net.URLConnection conn = createConn(dest,hash.length);
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(hash);
out.flush();
out.close();
private URLConnection createConn(URL dest,int len) throws IOException
{
URLConnection conn=dest.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-type","application/octet-stream");
conn.setRequestProperty("Data-length",""+ len );
conn.setRequestProperty("siteID",""+ siteID );
conn.setRequestProperty("totLen",""+ totLen );
return conn;
}
protected byte[] calculateHash(File inFile) throws IOException,
NoSuchAlgorithmException, FileNotFoundException
{
DigestInputStream dig = new DigestInputStream(new
BufferedInputStream
(new FileInputStream(inFile)), MessageDigest.getInstance("MD5"));
dig.on(true);
byte[] b = new byte[readBytes];
int len = 0;
long calculatedLength = 0;
while ((len = dig.read(b)) > 0)
{
calculatedLength += len;
//Every 5 runs, the percent is uploaded and applet repainted
if ((calculatedLength / len % 5) == 0)
{
percent = (int) Math.round(100.0 * calculatedLength /
inFile.length());
repaint(29,c1,210,40);
}
}
//This is the hash of the full file.
byte[] hash = dig.getMessageDigest().digest();
dig.close();
return hash;
}
-Sanjeev Kumar