下载jsp页面:
<meta http-equiv="Content-Type" content="text/html; charset=gbk"> <HTML> <HEAD> </HEAD> <BODY> <a href = "download.htm?id=${data.id}" >点击下载</a> </BODY> </HTML>
后台处理Action或Servlet:
TTrainingData trainingData=trainingDataService.findTrainingDataById(request.getParameter("id")); //要下载的资料对象
tring fileName=trainingData.getFileName()+trainingData.getPath().substring(trainingData.getPath().lastIndexOf('.')); //保存对话框中显示的文件名称
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename = "+new String(fileName.getBytes(),"ISO8859-1"));
FileInputStream fileInputStream=new FileInputStream(getServletContext().getRealPath(trainingData.getPath()));
byte[] b = new byte[1024];
int hasRead=0;
while ((hasRead=fileInputStream.read(b)) != -1) {
response.getOutputStream().write(b,0,hasRead);
}
fileInputStream.close();
response.getOutputStream().flush();
response.getOutputStream().close();
return null;