This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet to download  RSS feed

 
Satish Kulkarni
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have written a servlet to download a tar file.

The Servlet takes the parameters and create a tar file based on the parameters and then opens the file download ui for the browser. I am able to download the tar file and the contents of the tar file is ok.

(1) Refer to the code snippt below
In the "finally" section I am not able to delete the directories I have created.

/* Create tmp directories */
File tmpTarDir = createTmpDir(sessionInfo);
File tmpStoreDir = createStoreDir(sessionInfo);

String cmd[] = {
tarscript, RoboDir, AutoDir, Path, tmpTarDir.getAbsolutePath(),
tmpStoreDir.getAbsolutePath(), filename};

Runtime runtime = Runtime.getRuntime();

try {
Process process = runtime.exec(cmd);
process.waitFor();
}
catch (java.lang.InterruptedException intEx) {
logger.error(
"Interrupted exepction while executing or waiting for the process");
}
/* The script has created and stored the tar bundle is tmpStoreDir */
File downloadFile = new File(tmpStoreDir, filename + ".tar");
try {
sendDownloadFile(downloadFile, response);
}
catch (Exception e) {
logger.fatal("Download Exception in file: " + e);
}
finally {
if (tmpTarDir != null) {
File sessFile = tmpTarDir.getParentFile();
if (!sessFile.delete()) {
logger.error("Cannot delete the temporary Tar directory");
}
}

if (tmpStoreDir != null) {
if (tmpStoreDir.exists() && tmpStoreDir.isDirectory()) {
if (!tmpStoreDir.delete()) {
logger.error("Cannot delete the temporary Tar directory");
}
}
}
}


(2) Here I am getting the following IOException


private void sendDownloadFile(File downloadFile, HttpServletResponse response) {

try {

response.setContentType(contentType + "; name=\""
+ downloadFile.getName() + "\"");

Long length = new Long(downloadFile.length());
response.setContentLength(length.intValue());

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

OutputStream outStream =
new BufferedOutputStream(response.getOutputStream());
InputStream streamIn = new BufferedInputStream(
new FileInputStream(downloadFile));

byte[] tarBytes = new byte[1024];

while (streamIn.read(tarBytes) != -1) {
outStream.write(tarBytes);
}
outStream.flush();
streamIn.close();
}catch (FileNotFoundException fnfex) {
logger.fatal("File is MISSING!!");
}catch (IOException ioex) {
logger.fatal("IOException: Cannot read and write data");
}
catch (Exception e) {
logger.fatal("Download Exception: " + e);
}

}

Any help would be appreciated.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!