java怎样在服务器上创建文件夹

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()

}

}

File writeName = new File(你要存的地方)// 相对路径,如果没有则要建立一个新

//的.txt文件

writeName.createNewFile()// 创建新文件,有同名的文件的话直接覆盖

FileWriter writer = new FileWriter(writeName)

BufferedWriter out = new BufferedWriter(writer)

out.write(你要写入的信息);


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/285031.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-22
下一篇2023-04-22

发表评论

登录后才能评论

评论列表(0条)

    保存