JSP下载远程服务器文件,通过数据流的方式获取。后台代码如下:
@RequestMapping public void requestDownlod(HttpServletResponse response, String filePath){ RemoteDownLoadHelper helper = new RemoteDownLoadHelper(); String resultFileAbsolutePath = helper.getTargetFilePath(filePath); List<File> fileList = FileUtil.listFiles(resultFileAbsolutePath); for (File file : fileList) { logger.info(file.getAbsolutePath()); } File file = new File(resultFileAbsolutePath); InputStream inputStream = null; OutputStream outputStream = null; int BUFFER_SIZE = 1024; byte[] b = new byte[BUFFER_SIZE]; int len = 0; try { inputStream = new FileInputStream(file); outputStream = response.getOutputStream(); response.setContentType("application/force-download"); String filename = file.getName(); // filename = filename.substring(36, filename.length()); response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));//中文编码问题处理 response.setContentLength( (int) file.length( ) ); while((len = inputStream.read(b)) != -1){ outputStream.write(b, 0, len); } } catch (Exception e) { }finally{ IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); helper.deletePath(resultFileAbsolutePath); } }
RemoteDownLoadHelper类处理部分Controller的逻辑
public class RemoteDownLoadHelper { public String getTargetFilePath(String filePath){ //第一步,将传入的filePath进行gz压缩 String targetPath = new File(filePath).getParent(); String resultFileAbsolutePath = CompressFileUtil.compressedFile(filePath, targetPath); return resultFileAbsolutePath; } public boolean deletePath(String filePath){ return FileUtils.deleteQuietly(new File(filePath)); } }
CompressFileUtil为压缩工具类,先压缩再传输下载
public class CompressFileUtil { /** * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip * @param resourePath 源文件/文件夹 * @param targetPath 目的压缩文件保存路径 * @return void * @throws Exception */ public static String compressedFile(String resourcesPath, String targetPath) { File resourcesFile = new File(resourcesPath); // 源文件 ZipOutputStream out = null; File targetFile = new File(targetPath); // 目的 // 如果目的路径不存在,则新建 if (!targetFile.exists()) { targetFile.mkdirs(); } String targetName = resourcesFile.getName() + ".zip"; // 目的压缩文件名 FileOutputStream outputStream; String targetAbsolutePath = targetPath + "/" + targetName; try { outputStream = new FileOutputStream(targetAbsolutePath); out = new ZipOutputStream(new BufferedOutputStream(outputStream)); createCompressedFile(out, resourcesFile, ""); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ IOUtils.closeQuietly(out); } return targetAbsolutePath; } /** * @desc 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩 如果是文件,直接压缩 * @param out 输出流 * @param file 目标文件 * @return void * @throws Exception */ private static void createCompressedFile(ZipOutputStream out, File file, String dir) { // 如果当前的是文件夹,则进行进一步处理 FileInputStream fis = null; try { if (file.isDirectory()) { // 得到文件列表信息 File[] files = file.listFiles(); // 将文件夹添加到下一级打包目录 out.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; // 循环将文件夹中的文件打包 for (int i = 0; i < files.length; i++) { createCompressedFile(out, files[i], dir + files[i].getName()); // 递归处理 } } else { // 当前的是文件,打包处理 // 文件输入流 fis = new FileInputStream(file); out.putNextEntry(new ZipEntry(dir.equals("")?file.getName():dir));//兼容单独文件压缩时文件名为空字符串 // 进行写操作 int j = 0; byte[] buffer = new byte[1024]; while ((j = fis.read(buffer)) > 0) { out.write(buffer, 0, j); } } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭输入流 IOUtils.closeQuietly(fis); } } }
后台通过Reponse返回获取的数据流,在前端下载通过ajax+jquery下载
// Ajax 文件下载 jQuery.download = function (url, data, method) { // 获取url和data if (url && data) { // data 是 string 或者 array/object data = typeof data == 'string' ? data : jQuery.param(data); // 把参数组装成 form的 input var inputs = ''; jQuery.each(data.split('&'), function () { var pair = this.split('='); inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />'; }); // request发送请求 jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>') .appendTo('body').submit().remove(); }; };
JQuery的ajax函数的返回类型只有xml、text、json、html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载。但可以用js生成一个form,用这个form提交参数,并返回“流”类型的数据。
调用:
jQuery.download('<%=request.getContextPath()%>/requestDownlod.do','filePath='+filePath,'post');
参考:
http://www.16kan.com/question/detail/293686.html
相关推荐
JSP下载文件源代码,利用jsp-SmartUpload实现,代码简单,使用方便,调用简单。
jsp 下载文件 直接引用该文件即可
用java写的文件上传和下载,上传到硬盘物理路劲下,上传后用io读取txt文本文件,修改不能上传和下载是中文名称的bug,简单易用
实现jsp页面的下载功能,文档下载,文件下载,软件下载,测试过好用。
第一中使用二进制流的方式,这样被文件的文件可以是随意地址,二不必使用“/”开头的路径;...第二种使用RequestDispatcher下载文件,这中下载方式的局限是被下载文件的路径必须以“/”开头,即在项目的webRoot下
jsp文件下载功能,没有什么好说的,新手学习,呵呵。为什么为要二十个字呢。,。。
一个基于myeclipse开发平台的jsp文件上传、文件下载实例
jsp 实现 文件下载 jspsmartupload
java+jsp实现文件下载, 1、downloadtool.jsp为进入下载的页; 2、dodownloadtool.jsp实现下载;有详细的注释! 希望能帮到各位!
可直接导入eclipse中运行 博文链接:https://coderdream.iteye.com/blog/249824
使用jsp下载excel文件 JSP文件下载后台生成的excel文件到本地机。
完整的讲述了基于jsp的文件上传下载功能,具体讲解见http://blog.csdn.net/it_tingge/article/details/49025273
JSP实现文件上传与下载.pdf
用JSP下载word文件(不会直接用IE打开.rar
JSP实现文件的上传与下载,方便大家参考使用,实在方便,用到了文件上传组件,但是下载没有,大家参考参考学习学习,定会有所收获的。
在用JSP下载某些文件时,可能会碰到文件直接用IE打开了的情况。下面的方法可以解决这问题: downloadTest.jsp: ; charset=EUC-JP" language="java" %> *"%> *"%> *"%> <!DOCTYPE ...
使用JSP实现上传下载文件jspSmartUpload 里面讲述怎么用jsp实现
利用jsp实现文件上传下载,将文件上传到tomcat服务器之后下载。
用Jsp来实现文件下载功能的几种方式
java+jsp代码实现从FTP服务器上传下载文件。 里面给了java方法和jsp页面的代码。主要是实现从FTP服务器上面下载文件到自己本地文件夹下面。另外还给了几个网站供大家参考。