<% // 得到文件名字和路径
String filename = "GoldWave.rar";
String filepath = "d:\\GoldWave\\";
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename= \"" + filename + "\""); // 打开指定文件的流信息
java.io.FileInputStream fileInputStream =new java.io.FileInputStream(filepath + filename);
// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
%>
String filename = "GoldWave.rar";
String filepath = "d:\\GoldWave\\";
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename= \"" + filename + "\""); // 打开指定文件的流信息
java.io.FileInputStream fileInputStream =new java.io.FileInputStream(filepath + filename);
// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
%>
import javax.servlet.*;
import javax.servlet.http.*;import office.file.File;
import oracle.sql.BLOB;
import sun.jdbc.rowset.CachedRowSet;
import util.string.StringUtil;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DownloadFile extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType = request.getParameter("contentType");
if (contentType == null) {
contentType = "text/html; charset=GBK"; //默认情况下输出的类型为中文文本
}
String uploadId = request.getParameter("uploadId");
if (uploadId == null) {
uploadId = "";
}
String fileSerial = request.getParameter("fileSerial");
if(fileSerial==null) fileSerial = "";
String fileName = "FILE"+fileSerial;
File file = new File();
file.setFileSerial(fileSerial);
file.loadRecord();
String fileSuffix = file.getFileFormat();
Connection conn = null;
Statement ps = null;
ResultSet rs = null;
OpenDbBean db = new OpenDbBean();
try {
response.setContentType(contentType);
ServletOutputStream out = response.getOutputStream();
String sql =
"select * from uploadfiles where filename='" + fileName + "'";
System.out.println(sql);
conn = db.getConnection();
ps = conn.createStatement();
rs = ps.executeQuery(sql);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("binaryfile");
//发送文件名称和文件后缀
String head = fileName + "." + fileSuffix;
System.out.println(head);
response.setHeader(
"Content-disposition",
"attachment;filename=" + head);
int blobsize = (int) blob.length();
InputStream pi = blob.getBinaryStream();
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = pi.read(blobbytes)) != -1) {
out.write(blobbytes, 0, bytesRead);
}
pi.close();
out.flush();
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
db.CleanConnection(conn, ps, rs);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
//Clean up resources
public void destroy() {}
}