Java怎么通过远程读取流的方式将远程文件放到本地

Java怎么通过远程读取流的方式将远程文件放到本地,第1张

以下回答为本人意见,如果有误还请见谅。

java获取远程文件的方式在我的开发过程中使用过两种

1。通过http请求进行静态资源,首先确定文件的URL地址,然后通过URLConnection进行连接,然后通过读取连接中返回的InputStream,再通过文件输出流FileOutputStream进行存储(下载)。

2.通过FTP或SFTP进行远程文件的下载,具体实现有很多第三方的包,百度即可。

可以通过JDK自带的API实现,如下代码:

package com.cloudpower.util

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import sun.net.TelnetInputStream

import sun.net.TelnetOutputStream

import sun.net.ftp.FtpClient

/**

* Java自带的API对FTP的 *** 作

* @Title:Ftp.java

*/

public class Ftp {

/**

* 本地文件名

*/

private String localfilename

/**

* 远程文件名

*/

private String remotefilename

/**

* FTP客户端

*/

private FtpClient ftpClient

/**

* 服务器连接

* @param ip 服务器IP

* @param port 服务器端口

* @param user 用户名

* @param password 密码

* @param path 服务器路径

* @date 2012-7-11

*/

public void connectServer(String ip, int port, String user,

String password, String path) {

try {

/* ******连接服务器的两种方法*******/

//第一种方法

//ftpClient = new FtpClient()

//ftpClient.openServer(ip, port)

//第二种方法

ftpClient = new FtpClient(ip)

ftpClient.login(user, password)

// 设置成2进制传输

ftpClient.binary()

System.out.println("login success!")

if (path.length() != 0){

//把远程系统上的目录切换到参数path所指定的目录

ftpClient.cd(path)

}

ftpClient.binary()

} catch (IOException ex) {

ex.printStackTrace()

throw new RuntimeException(ex)

}

}

public void closeConnect() {

try {

ftpClient.closeServer()

System.out.println("disconnect success")

} catch (IOException ex) {

System.out.println("not disconnect")

ex.printStackTrace()

throw new RuntimeException(ex)

}

}

public void upload(String localFile, String remoteFile) {

this.localfilename = localFile

this.remotefilename = remoteFile

TelnetOutputStream os = null

FileInputStream is = null

try {

//将远程文件加入输出流中

os = ftpClient.put(this.remotefilename)

//获取本地文件的输入流

File file_in = new File(this.localfilename)

is = new FileInputStream(file_in)

//创建一个缓冲区

byte[] bytes = new byte[1024]

int c

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c)

}

System.out.println("upload success")

} catch (IOException ex) {

System.out.println("not upload")

ex.printStackTrace()

throw new RuntimeException(ex)

} finally{

try {

if(is != null){

is.close()

}

} catch (IOException e) {

e.printStackTrace()

} finally {

try {

if(os != null){

os.close()

}

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

public void download(String remoteFile, String localFile) {

TelnetInputStream is = null

FileOutputStream os = null

try {

//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。

is = ftpClient.get(remoteFile)

File file_in = new File(localFile)

os = new FileOutputStream(file_in)

byte[] bytes = new byte[1024]

int c

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c)

}

System.out.println("download success")

} catch (IOException ex) {

System.out.println("not download")

ex.printStackTrace()

throw new RuntimeException(ex)

} finally{

try {

if(is != null){

is.close()

}

} catch (IOException e) {

e.printStackTrace()

} finally {

try {

if(os != null){

os.close()

}

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

public static void main(String agrs[]) {

String filepath[] = { "/temp/aa.txt", "/temp/regist.log"}

String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"}

Ftp fu = new Ftp()

/*

* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器

*/

fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp")

//下载

for (int i = 0i <filepath.lengthi++) {

fu.download(filepath[i], localfilepath[i])

}

String localfile = "E:\\号码.txt"

String remotefile = "/temp/哈哈.txt"

//上传

fu.upload(localfile, remotefile)

fu.closeConnect()

}

}

你说的远程是Web服务器么?如果是Web服务器就用URLConnection,前提是知道远程文件的URL地址;如果是CS架构的服务就用Socket编程,前提是知道IP地址和服务端口以及请求的通讯报文格式。其他还有FTP服务等,情况不一样的。你需要把问题具体化。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/8003498.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存