Springmvc下载文件之限制同时下载数量

首先SpringMVC本身配置有下载文件的方法,就是这个"ResponseEntity<byte[]>",方法网上很多,可以搜一下,此次使用的是最基本的下载方法,使用文件输入输出流,因为SpringMVC自带的下载文件方法,是一个流,原理就是将文件一次性读取到内存中,文件过大就会造成内存溢出,并且,这个方法不能使用线程池检测执行的任务数量,具体原因我也不是很明白,所以自己使用了 FileInputStream进行下载,

使用线程池限制最大下载数量:

@RequestMapping(value="/toDownload.do")
	  public void toDownload( HttpServletRequest request,final HttpServletResponse response, String accessory) throws IOException {  	  
		    //得到该文件  由于在重写run方法使用该文件  所以需要加final 还有方法的形参加final同理
	        final File file = new File(accessory);
	        if(!file.exists()){
	            System.out.println("Have no such file!");
	            return;//文件不存在就退出方法
	        }
	        //使用线程池工厂创建连接池对象
	        ExecutorService es = Executors.newFixedThreadPool(3);
	        //获取正在运行的线程数量
		    int threadCount = ((ThreadPoolExecutor)es).getActiveCount(); 
		    //判断小于三时,方可进入下载程序
		    if(threadCount<3){
		    	//线程池对象调用线程方法   new  Runnable是重写该线程接口的run方法
		    	es.submit(new Runnable() {
		    		//以下为下载程序的代码
					@Override
					public void run() {
						String fileName=file.getName();
						//声明输入流
				        FileInputStream fileInputStream = null;
				        OutputStream outputStream = null;
				        //以下的所有的try...catch为系统提醒,直接点击生成的
						try {
							fileInputStream = new FileInputStream(file);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
				        //设置Http响应头告诉浏览器下载这个附件
				        try {
							response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8"));
						} catch (UnsupportedEncodingException e1) {
							e1.printStackTrace();
						}
				       
						try {
							outputStream = response.getOutputStream();
						} catch (IOException e) {
							e.printStackTrace();
						}
				        byte[] bytes = new byte[2048];
				        int len = 0;
				        try {
							while ((len = fileInputStream.read(bytes))>0){
							    outputStream.write(bytes,0,len);
							}
						} catch (IOException e) {
							e.printStackTrace();
						}finally {
							//finally是自己添的   下边的try还是系统提示的
							try {
								fileInputStream.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
					        try {
								outputStream.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				});
		    }else{
		    	//这是if判断自己写的
	        	throw new downlodeException("线路正忙请稍等");
	 }
此方法为我自己传递上来文件的路径,下边附上未使用线程池,使用静态变量,模拟获得请求数量的方法,	
 //下载附件
     /*private static int a=0;
	 @RequestMapping(value="/toDownload.do")
	 public void toDownload( HttpServletRequest request,HttpServletResponse response, String accessory) throws IOException {  	  
		    //得到该文件
	        File file = new File(accessory);
	        if(!file.exists()){
	            System.out.println("Have no such file!");
	            return;//文件不存在就退出方法
	        }
	        
	        if(a<3){
	        	a++;
	        System.out.println(a+"个进来的线程");	
            String fileName=file.getName();
	        FileInputStream fileInputStream = new FileInputStream(file);
	        //设置Http响应头告诉浏览器下载这个附件
	        response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8"));
	        OutputStream outputStream = response.getOutputStream();
	        byte[] bytes = new byte[2048];
	        int len = 0;
	        while ((len = fileInputStream.read(bytes))>0){
	            outputStream.write(bytes,0,len);
	        }
	        fileInputStream.close();
	        outputStream.close();
	        a--;
	        }else{
	        	throw new downlodeException("线路正忙请稍等");
	        }
		}
好了 有需要的朋友可以看看

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值