TOP NEWS

Welcome to my site... Have a nice day...

Showing posts with label Java. Show all posts

Monday, 9 September 2013

[JAVA] File upload with FTP


Hi :)

Today i will show you how a file transfer's a server with JAVA :)

Lets began :)

First you need that library:
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
 
 Apache Commons Net API
  

// Codes Began
 
 
package com.kacak41.FTP;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader {
 
 FTPClient ftp = null;
 
 public FTPUploader(String host, String user, String pwd) throws Exception{
  ftp = new FTPClient();
  ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  int reply;
  ftp.connect(host);
  reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
   ftp.disconnect();
   throw new Exception("Exception in connecting to FTP Server");
  }
  ftp.login(user, pwd);
  ftp.setFileType(FTP.BINARY_FILE_TYPE);
  ftp.enterLocalPassiveMode();
 }
 public void uploadFile(String localFileFullName, String fileName, String hostDir)
   throws Exception {
  try(InputStream input = new FileInputStream(new File(localFileFullName))){
  this.ftp.storeFile(hostDir + fileName, input);
  }
 }

 public void disconnect(){
  if (this.ftp.isConnected()) {
   try {
    this.ftp.logout();
    this.ftp.disconnect();
   } catch (IOException f) {
    // do nothing as file is already saved to server
   }
  }
 }
 public static void main(String[] args) throws Exception {
  System.out.println("Start");
  FTPUploader ftpUploader = new FTPUploader("ftp.yoursite/yourserver.com", "ftpUser", "ftpPassword");
  //FTP server path is relative. So if FTP account HOME directory is "/home/kacak41/public_html/" and you need to upload 
  // files to "/home/kacak41/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
  ftpUploader.uploadFile("D:\\kacak41\\images\\MyImage.png", "image.png", "/wp-content/uploads/image2/");
  ftpUploader.disconnect();
  System.out.println("Done");
 }

}
 
 
 
 
//Codes End 


With this code you can upload your files to your server or your site:)


Have a nice day...
02:57 Posted by Kacak41 0

Saturday, 7 September 2013

I Can Write Low Price Programs For You




If you want i can write for you programs with C, C# and Java Language's...

I don't want so high prices for this.

If you want to write a program please contact with me.

e-mail:murat.ozkl@gmail.com
05:14 Posted by Kacak41 0

Friday, 6 September 2013

[Java] Writing a IRC Bot


Hi! bro's and sis's..

today i will publish whit you a bot code Big Grin


First need a java library Smile)

i am using pircbot library for coding a irc bot!

Pircbot Library Home Page:http://www.jibble.org/pircbot.php
Pircbot Library Download Link:http://www.jibble.org/files/pircbot-1.5.0.zip

Download this library and open a project in netbeans.
[Image: iam8.png]

add library
[Image: H39adgt.png]

select your pircbot.jar file.

and open your main java file.


Codes:

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package botexample;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import org.jibble.pircbot.PircBot;

/**
*
* @author Murat
*/
public class BotExample extends PircBot {
    public BotExample() {
        this.setName("YourBotName");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         // genereate bot
        BotExample bot = new BotExample();
    
        // Enable debugging output.
        bot.setVerbose(true);
        try {
            // server name for connect
            bot.connect("irc.rizon.net");
        } catch (IOException ex) {
            Logger.getLogger(BotExample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IrcException ex) {
            Logger.getLogger(BotExample.class.getName()).log(Level.SEVERE, null, ex);
        }

        // channel name for connect
        bot.joinChannel("#channel");
    }

    @Override
    protected void onMessage(String channel, String sender, String login, String hostname, String message) {
        super.onMessage(channel, sender, login, hostname, message); //To change body of generated methods, choose Tools | Templates.

        //if msj= hi bot answer hi!+his nick
        if(message.equalsIgnoreCase("Hi"))
            sendMessage(channel, "Hi!"+sender);
        else if(message.startsWith(".kick") && sender.equalsIgnoreCase("Kacak41"))
        {
            String[] msg=message.split(" ");
            kick(channel, msg[1], "Kicked by Kacak41 request!");
            //first channel = which channel kicked user, msg[1] his nick 3. items= reason
        }
    }

    @Override
    protected void onJoin(String channel, String sender, String login, String hostname) {
        super.onJoin(channel, sender, login, hostname); //To change body of generated methods, choose Tools | Templates.
    sendMessage(channel, sender+" Welcome to Channel:"+channel);
    //sending welcome msj
    }

  

}



Screenshot:
[Image: NrMzsAS.png]
13:03 Posted by Kacak41 1

SSH Client With Java



Hi my friend i write a ssh client for my universty project..

i am using Jsch library http://www.jcraft.com/jsch/ you can find it here.!


here my program:Mo_Ssh


This is my main code.. Sorry their description turkish :S when i have a little time  i will translate to english.

package IsletimSistemleriProje;//this is project package

//adding JSCH library.
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;

public class IsletimSistemleriProje {

     public static void main(String[] arg){
   
    try{
        //Creating new object from JSCH library
      JSch jsch=new JSch();

      //host creating
      String host=null;
      //checking argumans if come from out
      if(arg.length>0){
          //adding coming arguman to host string
        host=arg[0];
      }
      else{
          //if no coming any argumans from out, we asking host name and username
        host=JOptionPane.showInputDialog("Enter username@hostname",
                                         "root"+"@83.136.252.56");
      }
      //we are parsing user name and host name
      String user=host.substring(0, host.indexOf('@'));
      host=host.substring(host.indexOf('@')+1);
     
      //we are creating new session on :22 port
      Session session=jsch.getSession(user, host, 22);
     
      //asking password
      String passwd = JOptionPane.showInputDialog("Enter password");
     
      //giving password to session
      session.setPassword(passwd);

      UserInfo ui = new MyUserInfo(){
          //if server have messaj we show them
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
        public boolean promptYesNo(String message){
            //Key accept message
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null,
                                               message,
                                               "Warning",
                                               JOptionPane.DEFAULT_OPTION,
                                               JOptionPane.WARNING_MESSAGE,
                                               null, options, options[0]);
          return foo==0;
        }

      };
      //adding session a user.
      session.setUserInfo(ui);
     
      //we are setting Connection time out.
      session.connect(30000); 

      //creting and starting shell
      Channel channel=session.openChannel("shell");

      //we set input values getting "System.in"
      channel.setInputStream(System.in);

      //we set output values show in "System.out"
      channel.setOutputStream(System.out);
      channel.connect(3*1000);
    }
    //Error handle
    catch(Exception e){
      System.out.println(e);
    }
  }

    
  public static abstract class MyUserInfo
                          implements UserInfo, UIKeyboardInteractive{
    public String getPassword(){ return null; }
    public boolean promptYesNo(String str){ return false; }
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return false; }
    public boolean promptPassword(String message){ return false; }
    public void showMessage(String message){ }
    public String[] promptKeyboardInteractive(String destination,
                                              String name,
                                              String instruction,
                                              String[] prompt,
                                              boolean[] echo){
      return null;
    }
  }
}
07:48 Posted by Kacak41 0