jsp中:
function upload(){
if(checkEmpty($("#ip")[0],"IP地址")&&checkEmpty($("#content")[0],"目录")&&checkEmpty($("#uploadFilePath")[0],"上传文件本地路径")){
$.post("gatherMainTainUpload.action",
{"gatherMainTain.ip":document.getElementById("ip").value,
"gatherMainTain.content":document.getElementById("content").value,
"gatherMainTain.uploadFilePath":document.getElementById("uploadFilePath").value
},
function(data){
alert($("#flag").val());
if(data+""=="true"){
if($("#flag").val()=="true"){
alert("上传文件成功!");
}else{
alert("目录不存在,无法上传文件,请重新输入!");
}
}else{
alert("上传文件失败!");
}
}
);
}
}
action中:
public String gatherMainTainUpload() throws Exception{
try{
this.gatherMainTains = this.gatherMainTainBeanRemote.selectByCondition(gatherMainTain);
GatherMainTainAction sf = new GatherMainTainAction();
String host = gatherMainTain.getIp();
int port = Integer.parseInt(gatherMainTains.get(0).getPort());
String username = gatherMainTains.get(0).getUserName();
String password = gatherMainTains.get(0).getPassWord();
String directory = gatherMainTain.getContent();
String uploadFile = gatherMainTain.getUploadFilePath();
ChannelSftp sftp=sf.connect(host,port,username,password);
//判断当前目录是否存在
this.gatherMainTain.setFlag("false");
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
if(sftpATTRS.isDir()){
this.gatherMainTain.setFlag("true");
}
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
this.gatherMainTain.setFlag("false");
}
}
// if(gatherMainTain.getFlag()=="true"){
// sftp.cd(directory);
// File file=new File(uploadFile);
//
// //uploadFile是本地文件所在绝对路径名
// sftp.put(new FileInputStream(file), file.getName());
// }
//
}
catch(Exception e){
e.printStackTrace();
throw(new GatherException(e));
}
return SUCCESS;
}
配置文件:
<action name="gatherMainTainUpload" class="system_gatherMainTainAction"
method="gatherMainTainUpload">
<result name="success">/ajaxTrue.jsp</result>