File Download Syntax:
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
String fileId = req. getParameter ("fileId ");
Req. setCharacterEncoding ("UTF-8 ");
Resp. setCharacterEncoding ("UTF-8 ");
If (StringUtils. isNotEmpty (fileId )){
FileAttach fileAttach = fileAttachService. get (new Long (fileId ));
String ext = fileAttach. getExt ();
If (ext. toLowerCase (). endsWith ("zip ")){
Resp. setContentType ("application/x-zip-compressed ");
} Else if (ext. toLowerCase (). endsWith ("rar ")){
Resp. setContentType ("application/octet-stream ");
} Else if (ext. toLowerCase (). endsWith ("doc ")){
Resp. setContentType ("application/msword ");
} Else if (ext. toLowerCase (). endsWith ("xls") | ext. toLowerCase (). endsWith ("csv ")){
Resp. setContentType ("application/ms-excel ");
} Else if (ext. toLowerCase (). endsWith ("pdf ")){
Resp. setContentType ("application/pdf ");
} Else {
Resp. setContentType ("application/x-msdownload ");
}
ServletOutputStream out = null;
Try {
Java. io. FileInputStream fileIn = new java. io. FileInputStream (getServletContext (). getRealPath ("/") + "/attachFiles/" + fileAttach. getFilePath ());
Resp. setHeader ("Content-Disposition", "attachment; filename =" + URLEncoder. encode (fileAttach. getFileName (), "UTF-8 "));
Out = resp. getOutputStream ();
Byte [] buff = new byte [1024];
Int leng = fileIn. read (buff );
While (leng> 0 ){
Out. write (buff, 0, leng );
Leng = fileIn. read (buff );
}
} Catch (Exception ex ){
Ex. printStackTrace ();
} Finally {
If (out! = Null ){
Try {
Out. flush ();
} Catch (IOException e ){
E. printStackTrace ();
}
Try {
Out. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}
}
Author: ERDP Technical Architecture"