当前位置:首页 » 文件管理 » javassh上传文件

javassh上传文件

发布时间: 2023-05-14 13:46:38

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

你可以使用JSch

JSch全称是“Java Secure Channel”

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

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

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

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

ChannelExecexec=(ChannelExec)session.openChannel("exec");
exec.setCommand("ifconfig");//这里是你要执唯败行的命令,部分命令中敏不支持,具体自己执行下
ByteArrayOutputStreambao=newByteArrayOutputStream();
exec.setOutputStream(bao);
ByteArrayOutputStreambaerr=newByteArrayOutputStream();
exec.setErrStream(baerr);
exec.connect();
while(!exec.isEOF())
;
Stringerrmsg=newString(baerr.toByteArray(),"utf-8");
if(StringUtils.notNull(errmsg)){
thrownewRuntimeException(errmsg);
}else{
System.out.println(newString(bao.toByteArray(),"utf-8"));
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//关闭session等操作
}

❷ SSH 图片上传问题 java.lang.NullPointerException myFileName为空,那个图片的名字获取不过来

private File myFile;
private String myFileName; ->应该为 private String myFileFileName;
上传时文件名应该是 文件+“FileName” 在这里就是 myFile+“FileName”

❸ 在Java项目中上传图片时如何使上传的图片自动保存到指定路径

用struts也可以实现 多文件上传

下面是我写的代码,

参数清谨中有要保存的目录

作为参考!

/*文答此基件目录*/
public static String [] fileArray={
"扒简logo.png",
"index.swf",
"OEMInfo.txt",
"favicon.ico"};

/**
* @author Caoshun
* @see 接收并保存文件
* */
public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){
String fileName="";
//获取表单中的文件资源
Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();
//遍历文件,并且循环保存
//当前处理文件序号
int file_num=1;
for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {

/*根据处理的当前文件下标,确定文件名*/
fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());
if (file != null && file.getFileSize() > 0) {
try {
//使用formfile.getInputStream()来获取一个文件的输入流进行保存。
//文件名
//String fileName = file.getFileName();
//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);
//文件大小
//int fileSize = file.getFileSize();
//文件流
InputStream is = file.getInputStream();
//将输入流保存到文件
//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中写入
File rf = new File(rootPath1);
FileOutputStream fos = null;
fos = new FileOutputStream(new File(rf, fileName));
byte[] b = new byte[10240];
int real = 0;
real = is.read(b);
while (real > 0) {
fos.write(b, 0, real);
real = is.read(b);
}

//往en中写入
File rf2 = new File(rootPath2);
InputStream is2 = file.getInputStream();
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(new File(rf2, fileName));
byte[] b2 = new byte[10240];
int real2 = 0;
real2 = is2.read(b2);
while (real2 > 0) {
fos2.write(b2, 0, real2);
real2 = is2.read(b2);
}

//关闭文件流
fos.close();
is.close();
fos2.close();
is2.close();
} catch (RuntimeException e1) {
e1.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
file.destroy();

}
file_num++;
}
}

❹ JAVA,SSH怎么获取图片上传时间

第一种,Java代码获取当前系统时间。
Date date =new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
Sting time = dateFormat.format(date);

第二种,sql语句直接插入当前时间。
instre into table values(now())
第三种,建表时候将时间字段默认是设置成 CURRENT_TIMESTAMP
例如: create table usertable(
`id` int(10) NOT NULL AUTO_INCREMENT,

`name` varchar(24) NOT NULL,

`createTime` datetime default CURRENT_TIMESTAMP
)
插入数据的时候,只需要写入 name 即可。id、createtime都自动生成数据。
insert into usertable (name) values("张三");
·
·

❺ Java通过SSH获取Linux文件出错

1
ssh
在cygwin中执行:$
ssh
username@remotehost
2
scp
命令scp基于ssh协议,可以将本地拆纤则文件拷贝到远程竖弯服务上的指定目旅棚录

热点内容
战舰少女r红茶脚本 发布:2025-02-12 04:05:05 浏览:465
峰火战国服务器什么时候开 发布:2025-02-12 03:56:31 浏览:175
电脑配置慢怎么解压 发布:2025-02-12 03:52:18 浏览:716
androidsdk功能 发布:2025-02-12 03:43:07 浏览:87
阿里云服务器可以访问外网吗 发布:2025-02-12 03:42:20 浏览:880
脚本的生命周期顺序 发布:2025-02-12 03:37:28 浏览:369
素数加密 发布:2025-02-12 03:37:27 浏览:803
ar源码 发布:2025-02-12 03:32:04 浏览:656
阅图文件夹 发布:2025-02-12 03:30:22 浏览:762
旧手机存储资料 发布:2025-02-12 03:29:42 浏览:472