PrintWriter out = null;
// 取得輸入參數
String did = req.getParameter("did");
String filename = req.getParameter("filename");
if (did == null) {// 無did
out.print("
return;
} else if (filename == null) {// 無filename
out.print("
return;
}
// Dao
DocAttDao docAttDao = DocAttDao.getInstance();
TblDocattId id = new TblDocattId(Integer.parseInt(did), filename);
TblDocatt tblDocAtt = new TblDocatt(id);
tblDocAtt = (TblDocatt) docAttDao.get(tblDocAtt);
try {
res.setHeader("Pragma", ""); // HTTP 1.0
res.setHeader("Cache-Control", ""); // HTTP 1.1
if (tblDocAtt != null) {
String fileurl = tblDocAtt.getUrl();
String agent = req.getHeader("User-Agent");
if (agent.indexOf("MSIE") > -1) {
// encoding filename for IE
filename = URLEncoder.encode(filename, "utf-8");
} else {
filename = new BCodec().encode(filename);
}
// 下載檔案
res.setContentType("application/octet-stream;charset=utf-8");
res.setHeader("Content-Disposition", "attachment; filename = " + filename);
// 打開指定文件的stream
File file = new File(fileurl);
FileInputStream bis = new FileInputStream(file);
OutputStream bos = res.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
readCount = bis.read(buff);
while (readCount != -1) {
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} else {
res.getWriter().print("
}
} catch (Exception e) {
e.printStackTrace();
}
}
沒有留言:
張貼留言