Opening Blog from Database in SQL Server
This is a class that will allow you to download files from an action class either from a file or from the database. The displayFileName is the filename that the user will see when they save their file. The code will check the file extension and set the content type accordingly. The other method is downloading a file from a blob. If you load the blob from the database and you need to open it, you will use this method. It will load the input stream from the blob method and download it accordingly.
Download.java
package com.delegata.common.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import javax.servlet.http.HttpServletResponse;
/**
* A utility for downloading a given file from file sytem.
*
* @author Greg Dias
*/
public class Download {
public static void downloadFile(HttpServletResponse res, String filename) // filename
// could
// full
// path!
throws Exception {
downloadFile(res, filename, filename);
}
/**
* Method used to download file from file system
*
* @param res
* HttpServletResponse
* @param filename
* File to download
* @param displayFileName
* Display name to show in file dialog box while downloading
* @throws Exception
* Exception
*/
public static void downloadFile(HttpServletResponse res, String filename,
String displayFileName) // filename could be full path!
throws Exception {
File file = new File(filename);
String contentType = "";
boolean ifXLSDOC = false;
// res.setContentType(URLConnection.guessContentTypeFromName(filename));
if (filename.toLowerCase().endsWith(".xls")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
}
if (filename.toLowerCase().endsWith(".xlt")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".csv")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".doc")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".rtf")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".pdf")) {
contentType = "application/pdf";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".ppt")) {
contentType = "application/ms-powerpoint";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".pot")) {
contentType = "application/ms-powerpoint";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".html")) {
contentType = "text/html";
} else if (filename.toLowerCase().endsWith(".htm")) {
contentType = "text/html";
} else if (filename.toLowerCase().endsWith(".gif")) {
contentType = "image/gif";
} else if (filename.toLowerCase().endsWith(".jpg")) {
contentType = "image/jpeg";
} else if (filename.toLowerCase().endsWith(".jpeg")) {
contentType = "image/jpeg";
} else if (filename.toLowerCase().endsWith(".dot")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".vsd")) {
contentType = "application/vnd.visio";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".mpp")) {
contentType = "application/vnd.ms-project";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".zip")) {
contentType = "application/ZIP";
} else if (filename.toLowerCase().endsWith(".txt")) {
contentType = "text/plain";
} else {
contentType = "application/octet-stream";
}
// Set the headers.
res.setContentType(contentType);
if (ifXLSDOC) {
res.addHeader("Content-Disposition", "attachment; filename="
displayFileName);
} else {
res.addHeader("Content-Disposition", "inline; filename="
displayFileName);
}
// Send the file.
OutputStream out = res.getOutputStream();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
res.setContentLength(in.available());
byte[] buf = new byte[4 * 1024]; // 4K byte buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
if (in != null) {
in.close();
}
out.flush();
out.close();
res.reset();
res.resetBuffer();
} catch (Exception e) {
}
}
public static void downloadFileBlob(Blob blob, HttpServletResponse res,
String filename, String displayFileName) // filename could be
// full path!
throws Exception {
//File file = new File(blob.toString());
String contentType = "";
boolean ifXLSDOC = false;
// res.setContentType(URLConnection.guessContentTypeFromName(filename));
if (filename.toLowerCase().endsWith(".xls")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
}
if (filename.toLowerCase().endsWith(".xlt")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".csv")) {
contentType = "application/vnd.ms-excel";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".doc")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".rtf")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".pdf")) {
contentType = "application/pdf";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".ppt")) {
contentType = "application/ms-powerpoint";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".pot")) {
contentType = "application/ms-powerpoint";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".html")) {
contentType = "text/html";
} else if (filename.toLowerCase().endsWith(".htm")) {
contentType = "text/html";
} else if (filename.toLowerCase().endsWith(".gif")) {
contentType = "image/gif";
} else if (filename.toLowerCase().endsWith(".jpg")) {
contentType = "image/jpeg";
} else if (filename.toLowerCase().endsWith(".jpeg")) {
contentType = "image/jpeg";
} else if (filename.toLowerCase().endsWith(".dot")) {
contentType = "application/msword";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".vsd")) {
contentType = "application/vnd.visio";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".mpp")) {
contentType = "application/vnd.ms-project";
ifXLSDOC = true;
} else if (filename.toLowerCase().endsWith(".zip")) {
contentType = "application/ZIP";
} else if (filename.toLowerCase().endsWith(".txt")) {
contentType = "text/plain";
} else {
contentType = "application/octet-stream";
}
// Set the headers.
res.setContentType(contentType);
if (ifXLSDOC) {
res.addHeader("Content-Disposition", "attachment; filename="
displayFileName);
} else {
res.addHeader("Content-Disposition", "inline; filename="
displayFileName);
}
// Send the file.
OutputStream out = res.getOutputStream();
InputStream in = null;
try {
in = blob.getBinaryStream();
Open Files from Blob or File using SQL and Struts 1
res.setContentLength(in.available());
byte[] buf = new byte[4 * 1024]; // 4K byte buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
if (in != null) {
in.close();
}
out.flush();
out.close();
res.reset();
res.resetBuffer();
} catch (Exception e) {
}
}
}

0 comments:
Post a Comment