Java web ftp

上传

/**
     * fit 文件上传
     * 
     * @param ip 服务器地址
     * @param userName  用户名
     * @param passWord  密 码
     * @param pathName  文件路径
     * @param filepath  源文件路径
     * @param nameFile  新文件名称
     */
    public static void testUpload(String ip, int port, String userName,
            String passWord,String pathName, byte[] bs, String nameFile) {
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        inputStream = new ByteArrayInputStream(bs);
        System.out.println("上传文件....");
        try {
            ftpClient.connect(ip);
            ftpClient.setDefaultPort(port);
            ftpClient.login(userName, passWord);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.makeDirectory(pathName);
            ftpClient.changeWorkingDirectory(pathName);
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
             if (bs != null) {
                 ftpClient.storeFile(nameFile,inputStream);
             }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            System.out.println("文件上传成功··");
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }

下载

/**
     * 文件下载
     * @param ip
     * @param port
     * @param userName
     * @param passWord
     * @param pathName
     * @param filepath
     * @param nameFile
     * @param request
     * @param response
     */
    public static void testDownload(String ip, int port, String userName,
            String passWord, String pathName, String FileName, String realName,HttpServletRequest request,
            HttpServletResponse response) {
        FTPClient ftpClient = new FTPClient();
        FileOutputStream fos = null;
        System.out.println("下载文件....");
        try {
            ftpClient.connect(ip);
            ftpClient.setDefaultPort(port);
            ftpClient.login(userName, passWord);
            ftpClient.changeWorkingDirectory(pathName);// 转移到FTP服务器目录
            int reply;
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
            reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
            }
            FTPFile[] fs = ftpClient.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(realName)) {
                    response.setContentType("text/html;charset=UTF-8");
                    request.setCharacterEncoding("UTF-8");
                    String checkBrowse =request.getHeader("user-agent");
                    //request.getHeader(“User-agent”)返回客户端浏览器的版本号、类型
                    String FIREFOX ="Firefox";
                    Pattern p =Pattern.compile(FIREFOX,Pattern.MULTILINE);  
                    Matcher m=p.matcher(checkBrowse);
                    boolean is1  = m.find(); 
                    String fileName = new String(FileName.getBytes("utf-8"), "ISO8859_1");  
                    if(!is1){
                        fileName = URLEncoder.encode(FileName,"UTF-8");         
                    }
                    response.setContentType("application/binary;charset=ISO8859-1");
                    response.setHeader("Content-disposition", "attachment; filename="+ fileName);
                    request.setCharacterEncoding("UTF-8");
                    response.setHeader("Content-Length", String.valueOf(ff.getSize()));
                    ftpClient.retrieveFile(ff.getName(),response.getOutputStream());
                    break;
                }
            }
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(fos);
            System.out.println("文件下载成功····");
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }

删除

/**
     * 
     * @param ip
     * @param port
     * @param userName
     * @param passWord
     * @param pathName
     * @param filepath
     * @param nameFile
     */
    public static void testDeleteFile(String ip, int port, String userName,
            String passWord, String pathName, String filepath, String nameFile) {
        FTPClient ftpClient = new FTPClient();
        System.out.println("删除文件.......");
        try {
            ftpClient.connect(ip);
            ftpClient.login(userName, passWord);
            ftpClient.changeWorkingDirectory(pathName);// 转移到FTP服务器目录
            ftpClient.deleteFile(nameFile);
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            System.out.println("文件删除成功····");
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }

下载前检测

public static boolean hashFIle(String ip, int port, String userName,
            String passWord, String ownerId, String FileName, String realName) {
        FTPClient ftpClient = new FTPClient();
        boolean has = false;
        System.out.println("检测文件....");
        try {
            ftpClient.connect(ip);
            ftpClient.setDefaultPort(port);
            ftpClient.login(userName, passWord);
            ftpClient.changeWorkingDirectory(ownerId);// 转移到FTP服务器目录
            // ftpClient.mkd(pathName);
            int reply;
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
            reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
            }
            FTPFile[] fs = ftpClient.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(realName)) {
                    has = true;
                }
            }
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            System.out.println("文件已经找到····");
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
        return has;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彳卸风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值