上传文件到服务器

来源:互联网 发布:sql2005新建数据库 编辑:程序博客网 时间:2024/04/29 03:28
/** *  * @param url   上传服务器IP地址  例如:210.30.108.10 * @param port上传服务器端口号  例如:21 * @param usernameFTP服务器用户名 * @param passwordFTP服务器用户密码 * @param remotePathFTP服务器上传路径目录 * @param fileNamePath要上传文件路径 * @param fileName要上传文件名 * @return */public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {    FTPClient ftpClient = new FTPClient();    FileInputStream fis = null;    String returnMessage = "0";    try {        ftpClient.connect(url, Integer.parseInt(port));        boolean loginResult = ftpClient.login(username, password);        int returnCode = ftpClient.getReplyCode();        if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功            ftpClient.makeDirectory(remotePath);            // 设置上传目录            ftpClient.changeWorkingDirectory(remotePath);            ftpClient.setBufferSize(1024);            ftpClient.setControlEncoding("UTF-8");            ftpClient.enterLocalPassiveMode();                    fis = new FileInputStream(fileNamePath + fileName+".txt");            Date now=new Date();         SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHH:mm:ss");         String name = dateFormat.format(now);         ftpClient.storeFile(fileName+","+name+".txt", fis);            fis.close();         returnMessage = "1";   //上传成功              } else {// 如果登录失败            System.out.println("登录失败");     returnMessage = "0";                     }                         } catch (IOException e) {        e.printStackTrace();        throw new RuntimeException("FTP客户端出错!", e);    } finally {   //    IOUtils.closeQuietly(fis);    try {        ftpClient.disconnect();    } catch (IOException e) {           e.printStackTrace();           throw new RuntimeException("关闭FTP连接发生异常!", e);       }    }    return returnMessage;   } 


在这里需要引用包  http://download.csdn.net/detail/u011930471/7128235

0 0
原创粉丝点击