當前位置:首頁 » 文件管理 » 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協議,可以將本地拆纖則文件拷貝到遠程豎彎服務上的指定目旅棚錄

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:610
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:900
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:594
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:778
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:696
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1024
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:267
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:127
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:816
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:729