工程的目录结构以及所需的jar文件如下:
其中tanchu.jpg图片如下:
1.新建一个jsp文件UpTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> #download{ border: 1px solid red; height:150px; width:300px; position:absolute; } </style> <script type="text/javascript"> //隐藏图片 function CloseCodeWindow() { var download = document.getElementById( "download"); download.style.display = "none"; } </script> </head> <body> <div id="download"> <img src="images/tanchu.jpg" width="300" height="124" border="0" usemap="#Map" /><!--通过usemap与下面的map元素关联--> </div> <!--map元素指定图片可点击区域--> <map name="Map" id="Map"> <area shape="rect" coords="73,52,225,98 " href="<%=request.getContextPath() %>/downloadpdfdata.jsp" /><!-- 这个坐标对应文字区域"点击下载该凭证" --> <area shape="rect" coords="263,5,293,33" href="javascript:CloseCodeWindow();"><!-- 这个坐标对应图片的关闭区域 --> </map> </body> </html>
2.新建downloadpdfdata.jsp
<% try{ pageContext.setAttribute("fileName", "D://HelloWorld.java"); //在D盘中存放HelloWorld.java文件,当然这个值可以通过session获取 String strFileName = (String)pageContext.getAttribute("fileName"); if(strFileName != null){ com.jspsmart.upload.SmartUpload su = new com.jspsmart.upload.SmartUpload(); su.initialize(pageContext); //使用jsp内置对象pageContext初始化 su.setContentDisposition(null); su.downloadFile(strFileName);//下载D盘下的HelloWorld.java文件 } }catch (Exception e){ out.write("<a>" + e.getMessage() + "</a>"); } %>