前些天做一个小项目需要在页面点击文件名实现功能,下面对实现的过程做简单的记录。 首先需要从静态页面获取需要下载文件的文件名,如代码中的attachment_1path,如果需要从数据库调用,则可以通过id查询出路径,下载功能的servlet如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String id=request.getParameter("id"); String attachment_1path=request.getParameter("attachment_1path"); System.out.println(attachment_1path);//检测attachment_1path String attachment_2path=request.getParameter("attachment_2path"); System.out.println("attachment_2-----"+attachment_2path); String attachment_3path=request.getParameter("attachment_3path"); System.out.println("-------3"+attachment_3path); String attachment=""; //判断三个下载链接中哪个被用户选择,然后把该值赋给attachment,以便实现代码公用 if(("".equals(attachment_3path)||("null".equals(attachment_3path))||(attachment_3path==null))&&(("".equals(attachment_2path))||("null".equals(attachment_2path))||(attachment_2path==null))){ attachment=attachment_1path; System.out.println(attachment_1path); }else if((("".equals(attachment_1path))||("null".equals(attachment_1path))||(attachment_1path==null))&&(("".equals(attachment_3path))||("null".equals(attachment_3path))||(attachment_3path==null))){ attachment=attachment_2path; }else if((("".equals(attachment_2path))||("null".equals(attachment_2path))||(attachment_2path==null))&&(("".equals(attachment_1path))||("null".equals(attachment_1path))||(attachment_1path==null))){ attachment=attachment_3path; System.out.println(attachment); } //实现文件下载动作 FileUtil fu=new FileUtil(); int r=fu.download(this.getServletContext(), response, attachment); } //下载文件的方法,参数Map存储下载文件信息,包括文件地址file public int download(ServletContext servletContext,HttpServletResponse response,String attachment) throws IOException{
BufferedInputStream bis = null; BufferedOutputStream bos = null; try { String filePath="/upload/attachments";//此处为下载文件的相对路径 String realPath=servletContext.getRealPath(filePath);//此处获得文件相对路径的绝对路径 response.setContentType("multipart/form-data;charset=utf-8");//设置类型,每次下载会判断下载文件的类型 response.setHeader("Content-disposition", "attachment; filename=" + attachment); //attachment为文件名 File file=new File(realPath+"\\"+attachment); FileInputStream filebuffer=new FileInputStream(file); bis = new BufferedInputStream(filebuffer); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[1024*1000]; int bytesRead; while ((bytesRead = bis.read(buff, 0, buff.length))!=-1) { //read()方法读入从字节buff的第一个字符到buff最后一个字节, //如果为第一次读入则返回-1,否则返回实际读入的字节数 bos.write(buff, 0, bytesRead); //实现字节刘写入 } } catch (IOException e) { return 0; } finally { bis.close(); bos.flush(); bos.close(); } return 1; }
以下为jsp页面获得数据库中存储的文件实际存储文件名,并判断:如果文件名为空则不显示,只有在不为空的时候才显示附件内容,并完成传递参数给servlet的任务,如下:
<% if(!"".equals(announ.getAttachment_1())&&announ.getAttachment_1()!=null&&!"null".equals(announ.getAttachment_1())){ %> 附件:<%=announ.getAttachment_1()%> <%} %> <% if(!"".equals(announ.getAttachment_2())&&announ.getAttachment_2()!=null&&!"null".equals(announ.getAttachment_2())){ %> 附件:<%=announ.getAttachment_2()%> <%} %>