页面jquery:
function downLoadFile(fileId){
$.get(base + "/problemDemand/downLoadFile?fileId=" + fileId, function(resp){
//$.get方法不需要再转一次json了
//var obj = $.parseJSON(resp);
if(resp.code == "1"){
//中间层正常执行
}else{
//中间层执行出现问题
}
});
//$.messager.alert('提示:','测试下载id为【'+fileId+'】的文件');
//return;
}
ACTION:
@At("/downLoadFile")
public Object downLoadFile(@Param("fileId")String fileId) {
Object result = null;
//封装参数
NutMap re = new NutMap();
re.setv("fileId", fileId);
//向ComService层传入功能号和NutMap
result = comServices.ExcuteServices("B01.01.16", re);
return result;
//return Json.toJson(result);
}
service层:
public Object downLoadFile(NutMap map) throws Exception {
long fileId = map.getLong("fileId");
UploadFile targetFile = dao.fetch(UploadFile.class, fileId);
//获得请求文件名
String filename = targetFile.getUuidName();
System.out.println("请求下载附件:"+filename);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = targetFile.getLocation() + targetFile.getUuidName() + targetFile.getExt();
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
return null;
}
问题:可以根据页面的get请求拿到文件、输出流也没问题,
但是怎么像servlet中一样,把流写到response中,返给浏览器呢?
浏览器的get方法的在resp.code == 1的情况下还需要做什么吗?