导航:首页 > IDC知识 > java上传文件到远程服务器

java上传文件到远程服务器

发布时间:2020-10-19 09:02:52

1、Java怎么读取远程服务器上的共享文件夹

ava获取远程文件的方式在我的开发过程中使用过两种
1。通过http请求进行静态资专源,首先确定文件的属URL地址,然后通过URLConnection进行连接,然后通过读取连接中返回的InputStream,再通过文件输出流FileOutputStream进行存储(下载)。
2.通过FTP或SFTP进行远程文件的下载,具体实现有很多第三方的包,百度即可。

2、java上传图片到远程服务器上,怎么解决呢?

不好实现,网上有方法说用FTP,但是不会用啊,找了一个public static void forcdt(String dir){InputStream in = null;OutputStream out = null;File localFile = new File(dir);try{//创建file类 传入本地文件路径//获得本地文件的名字String fileName = localFile.getName();//将本地文件的名字和远程目录的名字拼接在一起//确保上传后的文件于本地文件名字相同SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/");//创建读取缓冲流把本地的文件与程序连接在一起in = new BufferedInputStream(new FileInputStream(localFile));//创建一个写出缓冲流(注意jcifs-1.3.15.jar包 类名为Smb开头的类为控制远程共享计算机"io"包)//将远程的文件路径传入SmbFileOutputStream中 并用 缓冲流套接out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));//创建中转字节数组byte[] buffer = new byte[1024];while(in.read(buffer)!=-1){//in对象的read方法返回-1为 文件以读取完毕out.write(buffer);buffer = new byte[1024];}}catch(Exception e){e.printStackTrace();}finally{try{//注意用完操作io对象的方法后关闭这些资源,走则 造成文件上传失败等问题。!out.close();

3、如何使用java远程传输文件,client只提供ip\文件路径等参数,server端无需部署服务!

其实有几种方式的,
1 ftp传输应用情况,加入在linux系统端有一些文件需要下载到用户电脑client端,而linux系统又不是web服务器,那么可以通过java程序FTP方式登录到linux,读取文件转换为流输出到用户IE端, java访问Linux服务器读取文件 所需jar包:j2ssh-core-0.2.2.jar
2 socket方式,可以应用于比如server-client 聊天窗,传输文字;
3 http协议,这种就是最常用的了,比如打开IE下载,上传东西,java是通过jsp servlet来实现的,然后部署放在tomcat web 服务器上,在其他局域网环境下的电脑登录IE即可访问到。没有特殊jar,只用java servlet的jar即可。例子如附件,可能上传不成功哈,百度百度会有很多哈

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

可以通过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 = 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();
}
}

5、java如何执行远程服务器上的.sh文件

你可以使用JSch

JSch全称是“Java Secure Channel”

是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。同时也是支持执行命令;

以下是大概运行的代码,只是提供大致思路,可以去查官方API和demo

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

.......
try{
Session session = new JSch().getSession(user, ip, port);
session.setPassword(pwd);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("userauth.gssapi-with-mic", "no");
session.connect();

ChannelExec exec = (ChannelExec) session.openChannel("exec");
exec.setCommand("ifconfig");//这里是你要执行的命令,部分命令不支持,具体自己执行下
ByteArrayOutputStream bao = new ByteArrayOutputStream();
exec.setOutputStream(bao);
ByteArrayOutputStream baerr = new ByteArrayOutputStream();
exec.setErrStream(baerr);
exec.connect();
while (!exec.isEOF())
;
String errmsg = new String(baerr.toByteArray(), "utf-8");
if (StringUtils.notNull(errmsg)) {
throw new RuntimeException(errmsg);
} else {
System.out.println(new String(bao.toByteArray(), "utf-8"));
}
}catch(Exception e){
    e.printStackTrace();
}finally{
    //关闭session等操作
}

6、java如何实现远程访问服务器并将本地的class文件替换到服务器指定目录上???

使用Jenkins 来实现自动化部署

7、java上传图片到远程服务器上,怎么解决呢?

不好实现,网上有方法说用FTP,但是不会用啊,找了一个public static void forcdt(String dir){InputStream in = null;OutputStream out = null;File localFile = new File(dir);try{//创建file类 传入本地文件路径//获得本地文件的名字String fileName = localFile.getName();//将本地文件的名字和远程目录的名字拼接在一起//确保上传后的文件于本地文件名字相同SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/");//创建读取缓冲流把本地的文件与程序连接在一起in = new BufferedInputStream(new FileInputStream(localFile));//创建一个写出缓冲流(注意jcifs-1.3.15.jar包 类名为Smb开头的类为控制远程共享计算机"io"包)//将远程的文件路径传入SmbFileOutputStream中 并用 缓冲流套接out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));//创建中转字节数组byte[] buffer = new byte[1024];while(in.read(buffer)!=-1){//in对象的read方法返回-1为 文件以读取完毕out.write(buffer);buffer = new byte[1024];}}catch(Exception e){e.printStackTrace();}finally{try{//注意用完操作io对象的方法后关闭这些资源,走则 造成文件上传失败等问题。!out.close();in.close();

8、java 怎样追加文本到远程服务器上的文件,利用ftpclient

ftpClient.setRestartOffset(remoteSize);方法尝试把移动指针到远端文件的末尾,在用ftpClient.appendFileStream()获取输出流wirte();

9、Java怎么上传文件到远程Windows服务器

安装一个FTP就可以直接上传下载了,你可以去服务器厂商(正睿)的网上找找相关技术文档参考一下,很快就清楚了!

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

以下回答为本人意见,如果有误还请见谅。
java获取远程文件的方式在我的开发过程中使用过两种
1。通过http请求进行静态资源,首先确定文件的URL地址,然后通过URLConnection进行连接,然后通过读取连接中返回的InputStream,再通过文件输出流FileOutputStream进行存储(下载)。
2.通过FTP或SFTP进行远程文件的下载,具体实现有很多第三方的包,百度即可。

与java上传文件到远程服务器相关的知识