我在写一个按照绝对路径下载文件的操作以下是我控制器的文件
// 下载本地文件
String path = session.getServletContext().getRealPath("/");
StringBuffer pathName = new StringBuffer();
pathName.append(path).append(info.getAttachFilePosition());
String newpath = pathName.toString().replace("\\", "/");
System.out.println(newpath);
try {
// path是指欲下载的文件的路径。
File file = new File(newpath);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(newpath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
以下是我页面跳转的代码
$(function(){
$('#attchId').datagrid({
onDblClickRow: function(rowIndex) {
$('#attchId').datagrid('selectRow',rowIndex);
var item = $('#attchId').datagrid('getSelected');
var attachId = item.attachId;
var attachActName = item.attachActName;
var attachFilePosition = item.attachPosition;
var attachFileType = item.attachFileType;
var attachName = item.attachName;
var attachFileDesc = item.attachFileDesc;
var orgLifeId = $('#orgLifeId').val();
$.messager.confirm('提示', '确定要下载吗?', function(v) {
if(v){
$('#fileLoad').dialog({
title:'文件下载',
href:'orgLife/downLoadAttch.do',
width:200,
height:300,
modal:true,
shadow:true,
queryParams:{
attachId:attachId,
attachName:attachName,
attachActName:attachActName,
attachFileType:attachFileType,
attachFileDesc:attachFileDesc,
attachFilePosition:attachFilePosition,
orgLifeId:orgLifeId
}
});
}
});
$('#fileLoad').dialog('close');
}
});
});
我双击下载以后 文件下载下来了 但是在我打开的dialog 页面中直接打开显示了 而且还乱码了下面是乱码页面