一、jspjavascript
$(document).on("click",".download",function(){
var tContent=$(this).attr("va");
alert(tContent);
tContent=tContent.substr(7);
alert(tContent);
var url="/title_download";
location.href=url+"?tContent="+tContent;
})java
二、ajax動態添加里面ajax
<a href='javascript:void(0)' class='download' va='"+item.tContent+"'> 下載</a>session
三、controller頁面處理app
@RequestMapping("/title_download")
public void titleDownload(String tContent,HttpSession session,HttpServletResponse response) throws IOException{
//1路徑
String realpath=session.getServletContext().getRealPath(File.separator+"image"+File.separator+tContent);
//2輸入流
System.out.println(realpath);
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File(realpath)));
//3下載名稱
String filename="下載.jpg";
filename=new String(filename.getBytes("gbk"), "iso8859-1");
//4設置下載頭
response.addHeader("Content-Disposition", "attachment;filename="+filename);
//5設置文件contenttype類型
response.setContentType("multipart/form-data");
//6輸出流
BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
IOUtils.copy(inputStream, outputStream);
/* int len=0;
while ((len=inputStream.read())!=-1) {
outputStream.write(len);
}
outputStream.close();
inputStream.close();*/
}jsp