当前位置: 首页 > news >正文

servlet文件下载2(单文件下载和批量下载)

使用servlet完毕单文件下载和批量文件下载。批量下载的原理是先将文件打包成zip , 然后再下载。

之前也转载过一篇文件下载的博客,地址:http://blog.csdn.net/ch717828/article/details/42809999

这篇的demo要更简单一些,须要深入了解更參数含义的能够看第一篇博客

单文件下载:

<a href="servlet/SmartDownLoadFile?

filename=gui.jpg">附件</a>


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SmartDownLoadFile extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = getServletContext().getRealPath("/");
		String filename = request.getParameter("filename");
		File file = new File(path + filename);
		
		/*下载文件*/
		if(file.exists()){
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
			InputStream inputStream = new FileInputStream(file);
			ServletOutputStream ouputStream = response.getOutputStream();
			try{
				byte b[] = new byte[1024];
				int n ;
				while((n = inputStream.read(b)) != -1){
					ouputStream.write(b,0,n);
				}
			}finally
			{
				ouputStream.close();
				inputStream.close();
			}
		}
		else
		{
			System.out.println("文件不存在");
		}
	}
	
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req,resp);
	}

	

}

文件批量下载:
 <form action="servlet/BatchDownloadServlet" method="post">
  	 	<input type="checkbox"  name="filename" value="gui.jpg" />Image
  	 	<input type="checkbox"  name="filename" value="gui2.jpg" />Image2
  	 	<input type="checkbox"  name="filename" value="gui3.jpg" />Image3<br>
  	 	<input type="submit" value="下载">
  	 </form> 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class BatchDownloadServlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment;filename=test.zip"); //filename參数指定下载的文件名称
		String path = getServletContext().getRealPath("/");
		/*将要下载的文件打包成zip再下载*/
		String[] filenames  = request.getParameterValues("filename"); //获取要请求下载的文件名称
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); 
		for(String filename : filenames){
			File file = new File(path + filename);
			zos.putNextEntry(new ZipEntry(filename));
			FileInputStream fis = new FileInputStream(file);
			try
			{
				byte b[] = new byte[1024];
				int n = 0;
				while((n = fis.read(b)) != -1){
					zos.write(b, 0, n);
				}
			}finally
			{
				zos.flush();
				fis.close();
			}
		}
		zos.flush();
		zos.close();
		
		
	}

	

}

完整Demo:http://download.csdn.net/detail/ch717828/8837101

相关文章:

  • php 上传文件
  • 程序员工作中绕不开的9大问题,你遇到过几个?
  • Adobe将于2020年末停止对Flash的支持
  • quick-cocos2d-x教程9:实例之加上背景图片
  • iOS将数组中的内容分拼接成字符串
  • 如何使用阿里云虚拟主机搭建博客(二)搭建篇
  • create-react-app做的留言板
  • 中国式社交网络就一个“约”字而已
  • 测试人员的GitHub
  • 《企业级ios应用开发实战》一3.7 本章小结
  • GeekPwn黑客选手任意操纵智能烤箱 智能家居存隐患
  • 迈克菲报告指出网络威胁情报共享的阻碍
  • iOS开发-图片浏览器(优化)
  • 微软公司笔试题_附上源码解决
  • 本杰明·富兰克林会怎样学习编程?
  • ----------
  • @angular/forms 源码解析之双向绑定
  • 【css3】浏览器内核及其兼容性
  • Apache的基本使用
  • CSS实用技巧
  • CSS相对定位
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • github从入门到放弃(1)
  • HashMap剖析之内部结构
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • JavaScript标准库系列——Math对象和Date对象(二)
  • JavaScript函数式编程(一)
  • JavaScript类型识别
  • k8s 面向应用开发者的基础命令
  • SpingCloudBus整合RabbitMQ
  • Vue2.x学习三:事件处理生命周期钩子
  • Vue官网教程学习过程中值得记录的一些事情
  • yii2权限控制rbac之rule详细讲解
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 回顾 Swift 多平台移植进度 #2
  • 如何设计一个微型分布式架构?
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 世界上最简单的无等待算法(getAndIncrement)
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • 消息队列系列二(IOT中消息队列的应用)
  • 中文输入法与React文本输入框的问题与解决方案
  • raise 与 raise ... from 的区别
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (搬运以学习)flask 上下文的实现
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (十六)串口UART
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .form文件_一篇文章学会文件上传
  • .NET 4.0中的泛型协变和反变