//下载
$('#dasyszTable').on('click', '.download', function (e) {
var nRow = $(this).parents('tr')[0];
var aData = dasyszTable.fnGetData(nRow);
//插入日志
handleInsertRecord("进入法律、法规制度列表--附件下载");
var myform = $("<form></form>");
myform.attr('method','post');
myform.attr('action',BASEPATH + "flfgsysz/downloadFile");
var file1 = $(this).find("a").eq(0).context.innerHTML;
var fileName = $("<input type='hidden' name='fileName' />");
fileName.attr('value',file1+","+aData.disc+","+aData.num);
myform.append(fileName);
myform.appendTo('body').submit();
});
@ResponseBody
@RequestMapping(value = "/downloadFile", method = RequestMethod.POST)
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = request.getParameter("fileName");
String[] split = fileName.split(",");
String filePath = AddressUtil.getUpload_address() + split[1] + "\\" + split[2] + "_" + split[0];
// 读到流中
InputStream inStream = new FileInputStream(filePath);// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.setContentType("application/octet-stream; charset=utf-8");
fileName = URLEncoder.encode(split[0], "UTF-8");
if (fileName.length() > 150) {
String guessCharset = "gb2312"; /*根据request的locale 得出可能的编码,中文操作系统通常是gb2312*/
fileName = new String(fileName.getBytes(guessCharset), "ISO8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}