JQuery专栏之十六————服务器端文件下载的实现

16. 服务器端文件下载的实现。

从服务器端下载文件到客户端的实现方式有很多。本例从服务器端下载图片文件和wod文档至客户端。首先在表单中添加一个图形控件image1和学生组合框,每个学生的照片对应于一个图片文件,其文件名为学号与jpg的组合(如D2014540101.jpg)。图片文件下方添加一个学生简介的超链接,其链接文件名为学号与doc文件的组合(如D2014540101.doc)。下面给出三种不同的下载方式。

①直接使用location.href赋值方式,例如:

window.document.location.href='system/images/'+$("#stuid").combobox("getValue")+".jpg";

在有些游览器中,这种方式会在当前网页中打开下载文件,从而改变当前打开的网页地址。

②使用超链接<a href>方式将下载文件包含在链接地址中,例如:

<a href="mybase//D2014540101.doc">贾宝玉</a>

这种方式将服务器端文件打开后直接下载到本地游览器,而无需编写服务器端程序,但有些游览器会打开一个新的页面窗口。

③利用服务器端程序(Easyui_fileDownLoad.jsp)实现文件下载,可实现多个文件下载并命名下载文件。具体方法和程序如下:

客户端程序:

//指定源文件

var xsourcefilename=$("#image1").attr("src");

//指定目标文件

var xtargetfilename=$("#stuname").textbox("getValue")+".jpg";

//调用服务器端程序Easyui_fileDownLoad.jsp

var url='system//Easyui_fileDownLoad.jsp?path='+xsourcefilename+'&name='+xtargetfilename;

window.location.href=url;

服务器程序:Easyui_fileDownLoad.jsp代码:

<%@page contentType="application/x-msdownload" %>

<%@page language="java" import="java.util.*" pageEncoding="utf-8" %>

<%@page import="com.StringUtil" import="java.io.*" %>

<%@page import="java.io.BufferedInputStream" %>

<%@page import="java.io.BufferedOutputStream" %>

<%@page import="java.net.URLEncoder" %>

<%

          //输入两个参数:一个源文件名称sourcefile,一个目标文件名targetfile

          String root = application.getRealPath("/");

          String sourcefile = StringUtil.getToUtf8(request.getParameter("source"));

          String targetfile = StringUtil.getToUtf8(request.getParameter("target"));

          targetfile=URLEncoder.encode(targetfile,"UTF8");  //必须转换,否则中文乱码

          String result="";

          String s=root+sourcefile;

          String s1=s.substring(0,s.lastIndexOf("\\"));  //所在文件夹

          String s2=s.substring(s.lastIndexOf("\\")+1,s.length());

          File f=new File(s1,s2); 

          if (!f.exists()) {

                   result="源文件没有找到,下载失败!";

          }

          response.setHeader("Content-Type","application/x-msdownload;");

          response.setHeader("Content-disposition","attachment; filename="+targetfile+"");

          BufferedInputStream bis = null;

          BufferedOutputStream bos = null;

          try {

              bis = new BufferedInputStream(new FileInputStream(root+sourcefile));

              bos = new BufferedOutputStream(response.getOutputStream());

              byte[] buff = new byte[10 * 1024];

              int bytesRead;

              while ( -1 != (bytesRead = bis.read(buff, 0, buff.length))) {

                            bos.write(buff, 0, bytesRead);

                   }

              bos.flush();

          } catch (IOException ioe) {

                   System.out.println("下载错误:" + ioe);

                   result="文件下载错误:<BR>"+ioe;

          } finally {

                   if (bis != null) bis.close();

              if (bos != null) bos.close();

          }

          if (bos !=null ){

                   bos.flush();

                   bos.close();

          }

          bos = null;

          response.flushBuffer();   

          out.clear();

          out = pageContext.pushBody();

%>

需要指出,对于不同游览器和操作系统,文件下载、保存以及打开方式有所差异。

相关知识点:

①使用tool属性,在表单panel中添加自定义按钮(本例添加了3个按钮)

②图片等比例缩放函数resizeImage()。

③组合框的onSeelct事件。

④<div>中元素的去除(remove()方法)。

作业题:

1. 数据查询窗体的设计。

2.设计一个简单的计算器

猜你喜欢

转载自blog.csdn.net/qq_42618969/article/details/89202904