sun.net.ftp.FtpClient 操作FTP服务器上的文件和目录
其他
2019-04-22 19:43:05
阅读次数: 0
-
-
-
-
-
-
- private boolean createDir(String dir){
- try{
- ftpClient.ascii();
- StringTokenizer s = new StringTokenizer(dir, "/");
- s.countTokens();
- String pathName = ftpClient.pwd();
- while(s.hasMoreElements()){
- pathName = pathName + "/" + (String) s.nextElement();
- try {
- ftpClient.sendServer("MKD " + pathName + "\r\n");
- } catch (Exception e) {
- e = null;
- return false;
- }
- ftpClient.readServerResponse();
- }
- ftpClient.binary();
- return true;
- }catch (IOException e1){
- e1.printStackTrace();
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
- public boolean upload(String filename){
- String newname = "";
- if(filename.indexOf("/") > -1){
- newname = filename.substring(filename.lastIndexOf("/") + 1);
- }else{
- newname = filename;
- }
- return upload(filename, newname);
- }
-
-
-
-
-
-
-
-
-
- public boolean upload(String fileName, String newName){
- try{
- String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");
- File file_in = new File(savefilename);
- if(!file_in.exists()){
- throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
- }
- if(file_in.isDirectory()){
- upload(file_in.getPath(),newName,ftpClient.pwd());
- }else{
- uploadFile(file_in.getPath(),newName);
- }
-
- if(is != null){
- is.close();
- }
- if(os != null){
- os.close();
- }
- return true;
- }catch(Exception e){
- e.printStackTrace();
- System.err.println("Exception e in Ftp upload(): " + e.toString());
- return false;
- }finally{
- try{
- if(is != null){
- is.close();
- }
- if(os != null){
- os.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }
-
-
-
-
-
-
-
-
- private void upload(String fileName, String newName,String path) throws Exception{
- String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");
- File file_in = new File(savefilename);
- if(!file_in.exists()){
- throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
- }
- if(file_in.isDirectory()){
- if(!isDirExist(newName)){
- createDir(newName);
- }
- ftpClient.cd(newName);
- File sourceFile[] = file_in.listFiles();
- for(int i = 0; i < sourceFile.length; i++){
- if(!sourceFile[i].exists()){
- continue;
- }
- if(sourceFile[i].isDirectory()){
- this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"/"+newName);
- }else{
- this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());
- }
- }
- }else{
- uploadFile(file_in.getPath(),newName);
- }
- ftpClient.cd(path);
- }
-
-
-
-
-
-
-
-
- public long uploadFile(String filename, String newname) throws Exception{
- long result = 0;
- TelnetOutputStream os = null;
- FileInputStrea
-
-
-
-
-
-
- private boolean createDir(String dir){
- try{
- ftpClient.ascii();
- StringTokenizer s = new StringTokenizer(dir, "/");
- s.countTokens();
- String pathName = ftpClient.pwd();
- while(s.hasMoreElements()){
- pathName = pathName + "/" + (String) s.nextElement();
- try {
- ftpClient.sendServer("MKD " + pathName + "\r\n");
- } catch (Exception e) {
- e = null;
- return false;
- }
- ftpClient.readServerResponse();
- }
- ftpClient.binary();
- return true;
- }catch (IOException e1){
- e1.printStackTrace();
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
- public boolean upload(String filename){
- String newname = "";
- if(filename.indexOf("/") > -1){
- newname = filename.substring(filename.lastIndexOf("/") + 1);
- }else{
- newname = filename;
- }
- return upload(filename, newname);
- }
-
-
-
-
-
-
-
-
-
- public boolean upload(String fileName, String newName){
- try{
- String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");
- File file_in = new File(savefilename);
- if(!file_in.exists()){
- throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
- }
- if(file_in.isDirectory()){
- upload(file_in.getPath(),newName,ftpClient.pwd());
- }else{
- uploadFile(file_in.getPath(),newName);
- }
-
- if(is != null){
- is.close();
- }
- if(os != null){
- os.close();
- }
- return true;
- }catch(Exception e){
- e.printStackTrace();
- System.err.println("Exception e in Ftp upload(): " + e.toString());
- return false;
- }finally{
- try{
- if(is != null){
- is.close();
- }
- if(os != null){
- os.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }
-
-
-
-
-
-
-
-
- private void upload(String fileName, String newName,String path) throws Exception{
- String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");
- File file_in = new File(savefilename);
- if(!file_in.exists()){
- throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
- }
- if(file_in.isDirectory()){
- if(!isDirExist(newName)){
- createDir(newName);
- }
- ftpClient.cd(newName);
- File sourceFile[] = file_in.listFiles();
- for(int i = 0; i < sourceFile.length; i++){
- if(!sourceFile[i].exists()){
- continue;
- }
- if(sourceFile[i].isDirectory()){
- this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"/"+newName);
- }else{
- this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());
- }
- }
- }else{
- uploadFile(file_in.getPath(),newName);
- }
- ftpClient.cd(path);
- }
-
-
-
-
-
-
-
-
- public long uploadFile(String filename, String newname) throws Exception{
- long result = 0;
- TelnetOutputStream os = null;
- FileInputStrea
转载自blog.csdn.net/qq_44945073/article/details/89437483