当前页面: 开发资料首页 → J2SE 专题 → java可以访问其他机器的硬盘吗? 
java可以访问其他机器的硬盘吗? 
摘要: java可以访问其他机器的硬盘吗?  
用户名密码没有问题!
那就做个网络映射
服务器是linux
ding
ding
找一下,shh的包,直接可以运行命令
ganymed-ssh2-build211beta2.zip你可以下载这个包.
package com.corp.prj.util; 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; 
import org.apache.log4j.Logger; 
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; 
/**
 * Provides static methods for running SSH, scp as well as local commands.
 *log4j警告信息,不用管,必须用jdk1.5。
 */
public class CommandRunner {
 private static final Logger logger = Logger.getLogger(CommandRunner.class); 
 private CommandRunner() {
 } 
 /**
  * Get remote file through scp
   * @param host
   * @param username
   * @param password
   * @param remoteFile
   * @param localDir
   * @throws IOException
   */
 public static void scpGet(String host, String username, String password,
      String remoteFile, String localDir) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("spc [" + remoteFile + "] from " + host + " to " + localDir);
    }
    Connection conn = getOpenedConnection(host, username, password);
    SCPClient client = new SCPClient(conn);
    client.get(remoteFile, localDir);
    conn.close();
 } 
 /**
   * Put local file to remote machine.
   * @param host
   * @param username
   * @param password
   * @param localFile
   * @param remoteDir
  * @throws IOException
   */
 public static void scpPut(String host, String username, String password,
      String localFile, String remoteDir) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("spc [" + localFile + "] to " + host + remoteDir);
    }
    Connection conn = getOpenedConnection(host, username, password);
    SCPClient client = new SCPClient(conn);
    client.put(localFile, remoteDir);
    conn.close();
 } 
 /**
   * Run SSH command.
   * @param host
   * @param username
   * @param password
   * @param cmd
   * @return exit status
   * @throws IOException
   */
 public static int runSSH(String host, String username, String password,
      String cmd) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("running SSH cmd [" + cmd + "]");
    } 
    Connection conn = getOpenedConnection(host, username, password);
    Session sess = conn.openSession();
    sess.execCommand(cmd); 
    InputStream stdout = new StreamGobbler(sess.getStdout());
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 
    while (true) {
      // attention: do not comment this block, or you will hit NullPointerException
      // when you are trying to read exit status
      String line = br.readLine();
      if (line == null)
        break;
      if (logger.isDebugEnabled()) {
        logger.debug(line);
      }
System.out.println("line="+line);
    } 
    sess.close();
    conn.close();
    return sess.getExitStatus().intValue();
 } 
 /**
   * return a opened Connection
   * @param host
   * @param username
   * @param password
   * @return
   * @throws IOException
   */
 private static Connection getOpenedConnection(String host, String username,
      String password) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("connecting to " + host + " with user " + username
          + " and pwd " + password);
    } 
    Connection conn = new Connection(host);
    conn.connect(); // make sure the connection is opened
    boolean isAuthenticated = conn.authenticateWithPassword(username,
        password);
    if (isAuthenticated == false)
      throw new IOException("Authentication failed.");
    return conn;
 } 
 /**
   * Run local command
   * @param cmd
   * @return exit status
   * @throws IOException
   */
 public static int runLocal(String cmd) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("running local cmd [" + cmd + "]");
    } 
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(cmd); 
    InputStream stdout = new StreamGobbler(p.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 
    while (true) {
      String line = br.readLine();
      if (line == null)
        break;
      if (logger.isDebugEnabled()) {
        logger.debug(line);
      } 
    }
    return p.exitValue();
 }
 public static void main(String[] args) throws IOException{
System.out.println("i="+runSSH("192.168.1.6","root","123456","df"));
 }
}
顶楼上