Date: Wed, 4 Jul 2012 01:59:46 -0700
From:
cosmi...@gmail.comTo:
seleniu...@googlegroups.comSubject: Re: [selenium-users] Rename file in Selenium IDE
If you use Java, you can use following codes to download your file, then, how to change the file name is not a problem.
It needs the JSESSIONID, you can get it from cookie, the URI means the whole link to download the file, you can get it from the WebElement.
OutputStream os = null;
HttpClient httpClient = null;
try {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
httpClient = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet(uri);
// set the sessionid to the cookie.
if (!StringUtils.isEmpty(sessionId)) {
httpGet.setHeader("Cookie", "JSESSIONID=" + sessionId);
}
HttpResponse response = httpClient.execute(httpGet);
Header[] headers = response.getAllHeaders();
String fileName = "";
for (Header header : headers) {
if (header.getName().equals(CONTENT_DISPOSITION)) {
fileName = StringUtils.substringBetween(header.getValue(),
"\"", "\"");
}
}
if (!StringUtils.isEmpty(fileName)) {
fileName = savePath + File.separator + fileName;
} else {
fileName = savePath + File.separator
+ System.currentTimeMillis() + ".download";
}
InputStream is = response.getEntity().getContent();
File outFile = new File(fileName);
if (!outFile.exists()) {
outFile.createNewFile();
}
os = new FileOutputStream(outFile);
byte[] buf = new byte[4096];
int read;
while ((read = is.read(buf)) != -1) {
os.write(buf, 0, read);
}
return fileName;
} finally {
if (os != null)
os.close();
if (httpClient != null)
httpClient.getConnectionManager().shutdown();
}
with firefox you can use a combination of tow extensions to do this
1. flashgot extension to automatically perform the download action
2. downloadthemall to download to a specific folder and rename the file ( removing the timestamp)