一台服务器下载另一台服务器硬盘上的文件并打包成zip

服务器一

/** 
     * 将存放在sourceFilePath目录下的源文件,读取成字节码
     * @return 
     */ 
	public  List<Map<String,Object>> download(String sourceFilePath) {
		 File sourceFile = new File(sourceFilePath);  //读取的文件路径
		 FileInputStream fis = null;  
		 BufferedInputStream bis = null;
		 List<Map<String,Object>> list = new ArrayList<>();
		 if(sourceFile.exists() == false){  
	            log.debug("待压缩的文件目录:"+sourceFilePath+"不存在.");  
	            sourceFile.mkdir(); // 新建目录
	        }  
	        try {  
	                File[] sourceFiles = sourceFile.listFiles();  
	                if(null == sourceFiles || sourceFiles.length<1){  
	                    log.debug("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  
	                }else{  
	                   
	                    for(int i=0;i<sourceFiles.length;i++){  
	                    	//模拟压缩
	                    	Map<String,Object> resMap = new HashMap<String, Object>();
	                    	resMap.put("name", sourceFiles[i].getName());
	                        //读取待压缩的文件并写进压缩包里  
	                        fis = new FileInputStream(sourceFiles[i]); 
	                        int length = fis.available();
	                        bis = new BufferedInputStream(fis);  
	                        byte[] bufs = new byte[length];  
	                        int read = bis.read(bufs);
	                        resMap.put("byteData", bufs);
	                        list.add(resMap);
	                    }  
	            }  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	            throw new RuntimeException(e);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	            throw new RuntimeException(e);  
	        } finally{  
	            //关闭流  
	            try {  
	                if(null != bis) bis.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	                throw new RuntimeException(e);  
	            }  
	        }
		return list;
	}

服务器二

/**
	 * 许教授日志导出
	 * @param request
	 * @return
	 */
	@GetMapping("/znddDown")
	public void znddDown(HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<Map<String,Object>> list = getData(getParamMap(request));//获取日志数据
		download(list, "log", response);
	}
	
	
	//获取日志数据
	private List<Map<String,Object>> getData(Map<String, Object> params) {
		String Ip = commonConfig.getIp();
		String Port = commonConfig.getPort();
		String downloadPath = commonConfig.getDownloadPath();
		//4http调用远程服务器的方法
		String url ="http://"+Ip+ ":"+Port+"/zywlapi/logDownload/logDownload";
		Map<String, Object> headers = new HashMap<>();
			headers.put("Cache-Control", "no-cache");
			headers.put("Connection", "close");
			params.put("downloadPath",downloadPath+params.get("downloadPath"));	
		String outData = null;
		try {
			
			outData = HttpClientUtils.httpPostRequest(url, headers, params);
			
			log.debug("返回数据集合:"+outData);
		} catch (Exception e) {
			log.error(Arrays.toString(e.getStackTrace()));
		}
			//将字符串转成json对象
			JSONObject jsStr = JSONObject.fromObject(outData);
			//获取对象里的data  
			Object obj= jsStr.get("data");
			//将data转成json数组
			JSONArray jsonArray = JSONArray.fromObject(obj);
			//将json数组转成java集合
		    List<Map<String,Object>> list = (List<Map<String,Object> >) jsonArray.toCollection(jsonArray, HashMap.class);
		    return list;
	}
	 
	//输出数据
	private void download(List<Map<String,Object>> list,String fileName,HttpServletResponse response) {
        ZipOutputStream zos = null; 
        try {  
            	   OutputStream os = response.getOutputStream();
            	   /* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */
                   response.setContentType("multipart/form-data");
                   /* 设置文件头:最后一个参数是设置下载文件名 */
                   response.setHeader("Content-Disposition", "attachment;filename="+fileName+".zip");
                   // fos = new FileOutputStream(zipFile); 
                    zos = new ZipOutputStream(new BufferedOutputStream(os));  
                    for(int i=0;i<list.size();i++){  
                    	Map<String,Object> res = list.get(i);
                        //创建ZIP实体,并添加进压缩包  
                        ZipEntry zipEntry = new ZipEntry(res.get("name").toString());  
                        zos.putNextEntry(zipEntry);  
                        //读取待压缩的文件并写进压缩包里  
                        byte[] resByt=res.get("byteData").toString().getBytes();
                        //byte[]  resByt = (byte[]) res.get("byteData");
                        zos.write(resByt,0,resByt.length);   
                    }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        } catch (IOException e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        } finally{  
            //关闭流  
            try {  
                if(null != zos) zos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            }  
        } 
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值