kinlane
4/15/2012 - 9:52 PM

IDrive - EVS - REST API - downloadFile

IDrive - EVS - REST API - downloadFile

//Parameters to download/restore the latest version

String  params="uid="+strUsername+"&pwd="+strPassword+"&pvtkey="+strKey+"&p="+strPath;

// Parameters to download/restore specified version

String params="uid="+strUsername+"&pwd="+strPassword+"&pvtkey="+strKey+"&p="+strPath+"&version="+strVersion;

}

URL iburl = new URL("https://<server address>/evs/downloadFile");

URLConnection uc = iburl.openConnection();

uc.setDoOutput(true);

uc.setDoInput(true);

DataOutputStream dout =  new DataOutputStream (uc.getOutputStream ());

dout.writeBytes(params);

dout.flush();

dout.close();

//After sending data check for the status of response

String strStatus=uc.getHeaderField("RESTORE_STATUS");

System.out.println("strStatus:"+strStatus);

//If error then read the message and display the error

if(strStatus!=null && strStatus.equalsIgnoreCase("error"))

{

BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));

while ((str = in.readLine()) != null) {

result += str;

}

<%}

//Else download the file

else{

ServletContext  context  = getServletConfig().getServletContext();

String   mimetype = context.getMimeType( fileName );

System.out.println("mimetype::::"+mimetype);

response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );

//response.setContentLength( (int)f.length() );

response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileName + "\"" ); 

BufferedInputStream bis= new BufferedInputStream(uc.getInputStream());

ServletOutputStream  outs = response.getOutputStream();

byte[] buffer = new byte[30 * 1024];

int bytesRead = 0; 

while ((bytesRead = bis.read(buffer)) >= 0)

{

outs.write(buffer, 0, bytesRead);

}

outs.flush();

outs.close();

bis.close();

}