java web远程上传_javaweb 怎么样将本地文件传输到远程服务器

javaweb 怎么样将本地文件传输到远程服务器

关注:225  答案:1  mip版

解决时间 2021-01-13 14:44

e6cb1a03ad541b3098697807b7bf1798.png

提问者孤独深渊

2021-01-13 08:52

javaweb 怎么样将本地文件传输到远程服务器

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家噯ㄖ詘,美

2021-01-13 09:56

可以通过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;

public class Ftp {

private String localfilename;

private String remotefilename;

private FtpClient ftpClient;

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();

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

//下载

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

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

}

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

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

//上传

fu.upload(localfile, remotefile);

fu.closeConnect();

}

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

你可能感兴趣的:(java,web远程上传)