1.后台controller代码
/**
* 文件下载
*/
def fileDownload = {
def filePath = params.filePath //文件路径
def fileName = encode(params.fileName) //文件名
response.setHeader("Content-disposition", "attachment; filename=" + fileName)
response.contentType = "application/x-rarx-rar-compressed"
def out = response.outputStream
def inputStream = new FileInputStream(filePath)
byte[] buffer = new byte[1024]
int i = -1
while ((i = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, i)
}
out.flush()
out.close()
inputStream.close()
}
/**
* 字符编码
*/
final def encode(String value,String charSet='UTF-8'){
java.net.URLEncoder.encode(value, charSet)
}
2.前端gsp
/**
*下载
*/
function download(m,f){
//检查文件是否存在
var checkurl ="${contextPath}/fileUpload/checkFileExists?filePath="+encodeURIComponent(f);
j.ajax({
url:checkurl,
async:false,
success:function (dt) {
json = eval('('+dt+')');
if(json.success){
var url= "${contextPath}/fileUpload/fileDownload?filePath="+encodeURIComponent(f)+"&fileName="+encodeURIComponent(m);
window.open(url);
return false;
}else{
alert(json.msg);
}
}
});
}
413

被折叠的 条评论
为什么被折叠?



