java怎样在服务器上创建文件夹
1个回答
展开全部
可以FTP方式
准备条件:java实现ftp上传用到了commons-net-3.3.jar包
首先建立ftphost连接
public boolean connect(String path, String addr, int port, String username, String password) {
try {
//FTPClient ftp = new FTPHTTPClient(addr, port, username, password);
ftp = new FTPClient();
int reply;
ftp.connect(addr);
System.out.println("连接到:" + addr + ":" + port);
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP目标服务器积极拒绝.");
System.exit(1);
return false;
}else{
ftp.login(username, password);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(path);
System.out.println("已连接:" + addr + ":" + port);
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}
}
然后再利用ftpclient的makeDirectory方法创建文件夹
public void createDir(String dirname){
try{
ftp.makeDirectory(dirname);
System.out.println("在目标服务器上成功建立了文件夹: " + dirname);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
断开host连接
public void disconnect(){
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
最后是程序的调用方法
public static void main(String[] args) {
FtpUploadTest ftpupload = new FtpUploadTest();
if(ftpupload.connect("", "172.39.8.x", 20, "administrator", "abc@123")){
ftpupload.createDir("/UPLOAD");
ftpupload.disconnect();
}
}
准备条件:java实现ftp上传用到了commons-net-3.3.jar包
首先建立ftphost连接
public boolean connect(String path, String addr, int port, String username, String password) {
try {
//FTPClient ftp = new FTPHTTPClient(addr, port, username, password);
ftp = new FTPClient();
int reply;
ftp.connect(addr);
System.out.println("连接到:" + addr + ":" + port);
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP目标服务器积极拒绝.");
System.exit(1);
return false;
}else{
ftp.login(username, password);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(path);
System.out.println("已连接:" + addr + ":" + port);
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}
}
然后再利用ftpclient的makeDirectory方法创建文件夹
public void createDir(String dirname){
try{
ftp.makeDirectory(dirname);
System.out.println("在目标服务器上成功建立了文件夹: " + dirname);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
断开host连接
public void disconnect(){
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
最后是程序的调用方法
public static void main(String[] args) {
FtpUploadTest ftpupload = new FtpUploadTest();
if(ftpupload.connect("", "172.39.8.x", 20, "administrator", "abc@123")){
ftpupload.createDir("/UPLOAD");
ftpupload.disconnect();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询