java 远程服务器文件_JAVA下载远程Linux服务器的文件

RemoteAccessData.java

package com.yzj.demo;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.log4j.Logger;

import com.yzj.log.LogFactory;

import jcifs.smb.SmbFile;

import jcifs.smb.SmbFileInputStream;

import jcifs.smb.SmbFileOutputStream;

public class RemoteAccessData {

private static Logger logger = LogFactory.getInstance(RemoteAccessData.class);

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

smbGet("smb://username:password@11.132.3.13/prlife_ls_yanshou1_image/001/2013/12/05/10/00000053085901001/1/00000053085901001001.tif", "D:/download");

}

/**

* 路径格式:smb://192.168.75.204/test/新建 文本文档.txt

* smb://username:password@192.168.0.77/test

* @param remoteUrl

* 远程路径

* @param localDir

* 要写入的本地路径

*/

public static void smbGet(String remoteUrl, String localDir) {

InputStream in = null;

OutputStream out = null;

try {

SmbFile remoteFile = new SmbFile(remoteUrl);

if (remoteFile != null && remoteFile.exists()) {

String fileName = remoteFile.getName();

File localFile = new File(localDir + File.separator + fileName);

in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

out = new BufferedOutputStream(new FileOutputStream(localFile));

byte[] buffer = new byte[1024];

while (in.read(buffer) != -1) {

out.write(buffer);

buffer = new byte[1024];

}

} else {

// 文件不存在

logger.info(remoteUrl + " 文件不存在!");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

out.close();

out = null;

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 向共享目录上传文件

* @param remoteUrl

* @param localFilePath

*/

public static void smbPut(String remoteUrl, String localFilePath) {

InputStream in = null;

OutputStream out = null;

try {

File localFile = new File(localFilePath);

String fileName = localFile.getName();

SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);

in = new BufferedInputStream(new FileInputStream(localFile));

out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

byte[] buffer = new byte[1024];

while (in.read(buffer) != -1) {

out.write(buffer);

buffer = new byte[1024];

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

out.close();

out = null;

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

RemoteAccessData.java与jcifs-1.3.14.jar下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的SSH库来连接到远程Linux服务器,并从其中读取文件。 以下是一个简单的示例,演示如何使用JSch(一个Java SSH库)从Linux服务器上读取文件: ```java import com.jcraft.jsch.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class ReadRemoteFile { public static void main(String[] args) { String host = "remote-host"; String user = "username"; String password = "password"; String filePath = "/path/to/file"; try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand("cat " + filePath); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream input = channel.getInputStream(); channel.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); channel.disconnect(); session.disconnect(); } catch (JSchException | java.io.IOException e) { e.printStackTrace(); } } } ``` 在此示例中,我们使用JSch库连接到远程Linux服务器,并使用“cat”命令读取指定的文件。然后,我们将获得的内容打印到控制台上。 请注意,此示例中的SSH连接未进行身份验证。在生产环境中,您可能需要使用公钥/私钥身份验证或其他更安全的方法来连接到Linux服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值