ssm单文件下载ftp服务器到浏览器

来源:互联网 发布:js 函数对象的构造函数 编辑:程序博客网 时间:2024/05/17 01:37

前台页面

  • 注意:一定要用form的提交方式,否则浏览器接收到数据也不会显示下载。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>  <head>    <title>${title}</title>       <script type="text/javascript" src="${context_path}/resources/admin/js/jquery-1.8.3.min.js"></script>    <script type="text/javascript">         var api = frameElement.api;         function downLoad(fjid){                var param={"fjid":fjid,"uploadServerPath":'${uploadServerPath}'};                //定义一个form表单                var form=$("<form>");                form.attr("style","display:none");                form.attr("method","post");                form.attr("action",'${context_path}/lpt/xx/downLoad');                $.each(param,function(i,val){                    var input=$("<input>");                    input.attr("type","hidden");                    input.attr("name",i);                    input.attr("value",val);                    form.append(input);                });                //将表单放置在web中                $("body").append(form);                form.submit();         }    </script>  </head>  <body>    <div class="CHYI_Rtitle">            当前位置:<a>附件下载</a>             <a href="#" onclick="api.close();">返回</a>     </div>    <form id="mainform2" name="mainform2" method="get" action="">    <div class="CHYI_RTable">         <table border="0" cellpadding="0" cellspacing="0" width="100%">           <thead>             <tr class="head">                <td>文件名称</td>                <td>操作</td>              </tr>          </thead>          <tbody>           <#list fjList as data>             <tr class="active">                <td>${data.fjdz}</td>                 <td class="operate">                     <div class="CHYI_RBtn">                         <a href="#" onclick="downLoad('${data.fjid}')">下载</a>                     </div>                 </td>            </tr>           </#list>          </tbody>        </table>      </div>    </form>  </body></html>

后台代码

  1. 获取ftp服务器文件的InputStream值。
  2. 根据获取到的InputStream值 输出到浏览器。
  3. 最重要的一点,第一点中的file值大家一定要注意到。
    文件的ftp全路径是:/upload/fjdz/加班记录.txt
    错误file是/upload/fjdz/
    正确的file是upload/fjdz,前后一定没有”/”符号。
@RequestMapping(value="/downLoad",method=RequestMethod.POST)    public void downLoad(@RequestParam("fjid")String fjid,@RequestParam("uploadServerPath")String uploadServerPath,            HttpServletRequest request,HttpServletResponse response) throws IOException{        //1 获取ftp服务器文件的input值           InputStream input = FileUtils.downloadFile(file,fileName);        //2 下载        this.downloadFile(fileName,input,response);    }private void downloadFile(String fileName, InputStream input, HttpServletResponse response) throws IOException {    response.setContentType("application/octet-stream");    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));      OutputStream out = response.getOutputStream();      byte [] buffer = new byte[1024];      int len = 0;      while((len = input.read(buffer)) != -1){          out.write(buffer, 0, len);      }      input.close();    out.close();}

FileUtils工具类

public static  InputStream downloadFile(String file, String fileName){        FTPClient ftp = new FTPClient();        try {            int reply;            ftp.connect(SERVER_IP);            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器            ftp.login(USERNAME, PASSWORD);// 登录            reply = ftp.getReplyCode();            if (!FTPReply.isPositiveCompletion(reply)) {                ftp.disconnect();            }            ftp.setControlEncoding("UTF-8"); // 中文支持              ftp.setFileType(FTPClient.BINARY_FILE_TYPE);              ftp.enterLocalPassiveMode();              ftp.changeWorkingDirectory(file);// 转移到FTP服务器目录            FTPFile[] fs = ftp.listFiles();            for (FTPFile ff : fs) {                if (ff.getName().equals(fileName)) {                    InputStream input = ftp.retrieveFileStream(fileName);                    return input;                }            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (ftp.isConnected()) {                try {                     ftp.disconnect();                } catch (IOException ioe) {                }            }        }        return null;    }

效果图如下

这里写图片描述

原创粉丝点击