1.页面js中下载文件方法只有一句话:
window.open(url); url代表后台下载的链接地址,参数可以通过url传递过去.
2.后台下载文件的方法:
@RequestMapping(value = "/download.action")
public void download(HttpServletRequest request, HttpServletResponse response){
try{
String fileNo=request.getParameter("fileNo");
String fileName=request.getParameter("fileName");
SftpConfig config = SftpConfig.parseXml();
String path=config.getUploadFileRoad();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
File file=new File(path+"\\"+fileNo);
System.out.println(file.getAbsolutePath());
InputStream inputStream=new FileInputStream(file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
os.close();
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
}