Java实现从sftp批量下载文件并打包到浏览器

Java实现从sftp批量下载文件并打包到浏览器

首先声明: 该项目会先在本地创建一个临时文件夹,将文件下载到本地后打包发送到浏览器,发送完成后将本地文件删除。

jsp页面部分

<div class="queryCondition">
    <table class="query" width="100%" border="0" cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td class="Hint">时间:</td>
            <td class="nonHint"><input id="reportTime" class="laydate-icon" style="width: 130px; height: 27.5px;"></td>
            <td class="Hint">类型:</td>
            <td class="nonHint"><input id="reportName" class="laydate-icon" style="width: 130px; height: 27.5px;"></td>
        </tr>
        <tr style="text-align: center;vertical-align:middle;">
            <td colspan="6">
                <input type="button" class="btnReset" id="btnExportMx" value="下载sftp文件">
            </td>
        </tr>
        </tbody>
    </table>
</div>

js部分(这里可以根据前台选择类型下载不同的文件)

$(document).ready(function () {

	if ($('#reportName').length > 0) {
		$('#reportName').combobox({
			data: [{paramKey: 'Daily', paramValue: '日报'},
				{paramKey: 'Weekly', paramValue: '周报'}
			],
			valueField: 'paramKey',// 绑定字段ID
			textField: 'paramValue',// 绑定字段Name
			panelHeight: 'auto',// 自适应
			multiple: false,
			editable: false,
			width: 149,
			height: 27.5,
			formatter: function (row) {
				var opts = $(this).combobox('options');
				return '' + row[opts.textField];
			},
			value:'Daily'
		});
	}

	$('#btnExportMx').bind("click",function () {
		createExportTaskMx();
	})
})

//导出明细
function createExportTaskMx() {
	var params= getParamsAndType();
	var queryType='export';
	queryType&&(params.queryType=queryType);
	var fileForm=$('<form>');
	fileForm.attr('style', 'display: none');
	fileForm.attr('target', '');
	fileForm.attr('method', 'post');
	fileForm.attr('action', '/reportCenterWeb/intentionEvaluation/createExportTaskMx');
	for (var queryKey in params) {
		var eachVal = params[queryKey];
		var eachInput = $('<input>');
		eachInput.attr('type', 'hidden');
		eachInput.attr('name', queryKey);
		eachInput.attr('value', eachVal);
		fileForm.append(eachInput);
	}

	$('body').append(fileForm);

	fileForm.submit().remove();
}

//获取前台的时间和类型
function getParamsAndType() {
	var datas={};
	var reportTime = $('#reportTime').val();
	reportTime && (datas.reportTime=reportTime);
	var queryData = getQueryParams();
	var reportName = queryData.reportName;
	reportName && (datas.reportName=reportName);
	return datas;
}

Controller层

@Controller
@RequestMapping("intentionEvaluation")
public class intentionEvaluationController {
    
    @RequestMapping("createExportTaskMx")
    public void createExportTaskMx(Map<String,Object> param, HttpServletResponse response) {
        // 前台选择日期
        String clctDay = param.get("reportTime").toString().replaceAll("-","");
        SFTPUtilNew sftp = null;
        List<File> list;
        InputStream input = null;
        // 本地存放地址(先存放到本地,等跑完后会从本地删除)
        String localPath = "D:\\detail\\";
        //创建本地文件夹
        LocalUtil.createDir(localPath);
        // Sftp下载路径
        String sftpPathDay = "/home/detailDay/";
        String sftpPathWeek = "/home/detailWeek/";
        try
        {
            sftp = new SFTPUtilNew();
            sftp.connect();
            // 下载(可以根据前台选择的类型下载不同的文件)
            if(param.get("reportName").equals("Daily")){
                list = sftp.batchDownLoadFile(sftpPathDay, localPath, "日报", clctDay + ".csv", false);
                //打包
                SFTPUtilNew.zipFiles(list,"日报" + clctDay,response);
            }
            else if(param.get("reportName").equals("Weekly")){
                list = sftp.batchDownLoadFile(sftpPathWeek, localPath, "周报", clctDay + ".csv", false);
                //打包
                SFTPUtilNew.zipFiles(list,"周报" + clctDay,response);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (input != null) {
                try {
                    input.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            sftp.disconnect();
            //执行完成后删除本地文件及文件夹
            LocalUtil.delFolder(localPath);
        }
    }
}

下面写一个stfp下载的工具类(支持批量下载和打包)

package com.fine.tools.utils;

import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;

import javax.servlet.http.HttpServletResponse;

public class SFTPUtilNew {
	private String host = "127.0.0.1";//服务器连接ip
	private String username = "root";//用户名
	private String password = "123";//密码
	private int port = 22;//端口号
	private ChannelSftp sftp = null;
	private Session sshSession = null;

	public SFTPUtilNew(){}

	public SFTPUtilNew(String host, int port, String username, String password)
	{
		this.host = host;
		this.username = username;
		this.password = password;
		this.port = port;
	}

	public SFTPUtilNew(String host, String username, String password)
	{
		this.host = host;
		this.username = username;
		this.password = password;
	}

	/**
	 * 通过SFTP连接服务器
	 */
	public void connect()
	{
		try
		{
			JSch jsch = new JSch();
			jsch.getSession(username, host, port);
			sshSession = jsch.getSession(username, host, port);
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			sshSession.connect();
			Channel channel = sshSession.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 关闭连接
	 */
	public void disconnect()
	{
		if (this.sftp != null)
		{
			if (this.sftp.isConnected())
			{
				this.sftp.disconnect();
			}
		}
		if (this.sshSession != null)
		{
			if (this.sshSession.isConnected())
			{
				this.sshSession.disconnect();
			}
		}
	}

	/**
	 * 批量下载文件
	 * @param remotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)
	 * @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\)
	 * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
	 * @param fileEndFormat:下载文件格式(文件格式)
	 * @param del:下载后是否删除sftp文件
	 * @return
	 */
	public List<File> batchDownLoadFile(String remotePath, String localPath,
										  String fileFormat, String fileEndFormat, boolean del)
	{
		List<File> fileList = new ArrayList<File>();
		File file = null;
		try
		{
			Vector v = listFiles(remotePath);
			if (v.size() > 0)
			{
				Iterator it = v.iterator();
				while (it.hasNext())
				{
					LsEntry entry = (LsEntry) it.next();
					String filename = entry.getFilename();
					SftpATTRS attrs = entry.getAttrs();
					if (!attrs.isDir())
					{
						fileFormat = fileFormat == null ? "" : fileFormat
								.trim();
						fileEndFormat = fileEndFormat == null ? ""
								: fileEndFormat.trim();
						// 三种情况
						if (fileFormat.length() > 0 && fileEndFormat.length() > 0)
						{
							if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat))
							{
								file = downloadFile(remotePath, filename,localPath, filename);
								if (file!=null)
								{
									fileList.add(file);
									if (file!=null && del)
									{
										deleteSFTP(remotePath, filename);
									}
								}
							}
						}
						else if (fileFormat.length() > 0 && "".equals(fileEndFormat))
						{
							if (filename.startsWith(fileFormat))
							{
								file = downloadFile(remotePath, filename, localPath, filename);
								if (file!=null)
								{
									fileList.add(file);
									if (file!=null && del)
									{
										deleteSFTP(remotePath, filename);
									}
								}
							}
						}
						else if (fileEndFormat.length() > 0 && "".equals(fileFormat))
						{
							if (filename.endsWith(fileEndFormat))
							{
								file = downloadFile(remotePath, filename,localPath, filename);
								if (file!=null)
								{
									fileList.add(file);
									if (file!=null && del)
									{
										deleteSFTP(remotePath, filename);
									}
								}
							}
						}
						else
						{
							file = downloadFile(remotePath, filename,localPath, filename);
							if (file!=null)
							{
								fileList.add(file);
								if (file!=null && del)
								{
									deleteSFTP(remotePath, filename);
								}
							}
						}
					}
				}
			}
		}
		catch (SftpException e)
		{
			e.printStackTrace();
		}
		return fileList;
	}

	/**
	 * 下载单个文件
	 * @param remotePath:远程下载目录(以路径符号结束)
	 * @param remoteFileName:下载文件名
	 * @param localPath:本地保存目录(以路径符号结束)
	 * @param localFileName:保存文件名
	 * @return
	 */
	public File downloadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
	{
		FileOutputStream fieloutput = null;
		File file = null;
		try
		{
			file = new File(localPath + localFileName);
			fieloutput = new FileOutputStream(file);
			sftp.get(remotePath + remoteFileName, fieloutput);
			fieloutput.close();
			return file;
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (SftpException e)
		{
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			if (null != fieloutput)
			{
				try
				{
					fieloutput.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	public InputStream downloadStream(String directory, String downloadFile){
		try{
			if(directory != null && !"".equals(directory)){
				sftp.cd(directory);
			}
			return sftp.get(downloadFile);
		}catch (SftpException e){
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 上传单个文件
	 * @param remotePath:远程保存目录
	 * @param remoteFileName:保存文件名
	 * @param localPath:本地上传目录(以路径符号结束)
	 * @param localFileName:上传的文件名
	 * @return
	 */
	public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
	{
		FileInputStream in = null;
		try
		{
			createDir(remotePath);
			File file = new File(localPath + localFileName);
			in = new FileInputStream(file);
			sftp.put(in, remoteFileName);
			return true;
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (SftpException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return false;
	}

	/**
	 * 批量上传文件
	 * @param remotePath:远程保存目录
	 * @param localPath:本地上传目录(以路径符号结束)
	 * @param del:上传后是否删除本地文件
	 * @return
	 */
	public boolean bacthUploadFile(String remotePath, String localPath,
								   boolean del)
	{
		try
		{
			connect();
			File file = new File(localPath);
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++)
			{
				if (files[i].isFile()
						&& files[i].getName().indexOf("bak") == -1)
				{
					if (this.uploadFile(remotePath, files[i].getName(),
							localPath, files[i].getName())
							&& del)
					{
						deleteFile(localPath + files[i].getName());
					}
				}
			}
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			this.disconnect();
		}
		return false;

	}

	/**
	 * 删除本地文件
	 * @param filePath
	 * @return
	 */
	public boolean deleteFile(String filePath)
	{
		File file = new File(filePath);
		if (!file.exists())
		{
			return false;
		}

		if (!file.isFile())
		{
			return false;
		}
		boolean rs = file.delete();
		return rs;
	}

	/**
	 * 创建目录
	 * @param createpath
	 * @return
	 */
	public boolean createDir(String createpath)
	{
		try
		{
			if (isDirExist(createpath))
			{
				this.sftp.cd(createpath);
				return true;
			}
			String pathArry[] = createpath.split("/");
			StringBuffer filePath = new StringBuffer("/");
			for (String path : pathArry)
			{
				if (path.equals(""))
				{
					continue;
				}
				filePath.append(path + "/");
				if (isDirExist(filePath.toString()))
				{
					sftp.cd(filePath.toString());
				}
				else
				{
					// 建立目录
					sftp.mkdir(filePath.toString());
					// 进入并设置为当前目录
					sftp.cd(filePath.toString());
				}

			}
			this.sftp.cd(createpath);
			return true;
		}
		catch (SftpException e)
		{
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 判断目录是否存在
	 * @param directory
	 * @return
	 */
	public boolean isDirExist(String directory)
	{
		boolean isDirExistFlag = false;
		try
		{
			SftpATTRS sftpATTRS = sftp.lstat(directory);
			isDirExistFlag = true;
			return sftpATTRS.isDir();
		}
		catch (Exception e)
		{
			if (e.getMessage().toLowerCase().equals("no such file"))
			{
				isDirExistFlag = false;
			}
		}
		return isDirExistFlag;
	}

	/**
	 * 删除stfp文件
	 * @param directory:要删除文件所在目录
	 * @param deleteFile:要删除的文件
	 */
	public void deleteSFTP(String directory, String deleteFile)
	{
		try
		{
			sftp.rm(directory + deleteFile);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 将文件打包
	 * @param srcfile:文件集合
	 * @param zipFileName:生成的文件名
	 */
	public static void zipFiles(List<File> srcfile, String zipFileName, HttpServletResponse response) throws IOException {
		byte[] buf = new byte[1024];
		// 获取输出流
		BufferedOutputStream bos = null;
		try {
			bos = new BufferedOutputStream(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		FileInputStream in = null;
		ZipOutputStream out = null;
		try {
			response.reset();
			// 不同类型的文件对应不同的MIME类型
			response.setContentType("application/x-msdownload");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipFileName + ".zip", "UTF-8"));

			// ZipOutputStream类:完成文件或文件夹的压缩
			out = new ZipOutputStream(bos);
			for (int i = 0; i < srcfile.size(); i++) {
				in = new FileInputStream(srcfile.get(i));
				// 给列表中的文件单独命名
				out.putNextEntry(new ZipEntry(srcfile.get(i).getName()));
				int len = -1;
				while ((len = in.read(buf)) != -1) {
					out.write(buf, 0, len);
				}
			}
			out.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) in.close();
			if (out != null) out.close();
		}
	}

	/**
	 * 列出目录下的文件
	 *
	 * @param directory:要列出的目录
	 * @return
	 * @throws SftpException
	 */
	public Vector listFiles(String directory) throws SftpException
	{
		return sftp.ls(directory);
	}

	public String getHost()
	{
		return host;
	}

	public void setHost(String host)
	{
		this.host = host;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public int getPort()
	{
		return port;
	}

	public void setPort(int port)
	{
		this.port = port;
	}

	public ChannelSftp getSftp()
	{
		return sftp;
	}

	public void setSftp(ChannelSftp sftp)
	{
		this.sftp = sftp;
	}

}

下面写一个操作本地文件的工具类

package com.fine.tools.utils;

import java.io.File;

/*
 * 操作本地文件工具类
 */
public class LocalUtil {

    //创建本地文件夹
    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            return true;
        } else {
            return false;
        }
    }

    //删除文件夹
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除指定文件夹下的所有文件
    public static boolean delAllFile(String path) throws InterruptedException {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                //由于关闭io流后文件仍然被占用,因此直接采用gc垃圾回收等待两秒删除
                System.gc();
                Thread.sleep(2000);
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值