FTP upload

xiaoxiao2021-03-06  66

In a general web application, file upload can be divided into two types (FTP upload and HTTP upload). Where HTTP upload is an interface to the type in the Form form of the page, when the form is submitted, the file referred to in the file will be written to the Request object. So on the server, the user can get the file as long as the RFC1867 protocol parses the data stream in the request, but the speed of upload is very slow, and the size of the file is required. In contrast, FTP uploads are superior. He is the FTP client interacts directly with the FTP server and creates the file to the FTP server. Of course, the FTP client has been implemented. Below we use third-party FTP clients to implement FTP upload.

First, download the FTP client from www.enterprisedt.com. The entire application is equally five classes to make upload.java, fileupload.java, directoryupload.java, directory.java, uploadextion.java. I hope everyone will improve.

/ * * @ (#) UPLOAD.JAVA 1.0 04/11/17 * Implement the initial initialization in advance * / package com.head.upload;

// Call the third-party FTP client, you can download Import com.enterprisedt.net.ftp. *; Import java.io. *; in www.enterprisedt.com;

Public class upload {static ftpclient ftpclient; static string path;

// FTP server IP address, login user and password string remoteaddress = "127.0.0.1", username = "xie", password = "xie"; upload () {} Upload (String path) {this.path = path; } public boolean config (String fileName) throws UploadException {// read remote address, user name and password (not implemented) return false from the configuration file;} public void init () throws UploadException {try {ftpClient = new FTPClient (this .remoteAddress); ftpClient.login (this.userName, this.password); ftpClient.setConnectMode (FTPConnectMode.PASV); ftpClient.setType (FTPTransferType.BINARY);} catch (Exception e) {throw new UploadException (e.toString ( ), "Upload.init ()");}}

Public Boolean Upload () throws uploadexception {file file = null; boolean flag = false; type = new file (); if (file.isfile () && new fileupload (). UploadFile ()) Flag = True; Else IF (file.isdirectory () && new directoryupload (). UPLOADFILE (PATH)) Flag = true;} catch (e) {throw new uploadext (e.tostring (), "Upload.upload ()" );} finally {clear (ftpClient);} return flag;} boolean clear (FTPClient ftpClient) throws UploadException {try {ftpClient.quit (); return true;} catch (Exception e) {throw new UploadException (e.toString ( ), "Upload.clear (ftpclient)");}}}} / * * @ (#) fileupload.java 1.0 04/11/17 * * Upload a single file * / package com.head.upload;

Import com.enterprisedt.net.ftp. *; import java.io. *;

Public class fileupload extends upload {

FileUpload () {} public boolean uploadFile () throws UploadException {// the name of file will be changed to avoid iteration String fileName = path.substring (path.lastIndexOf ( "//") 1); try {ftpClient.put (path, fileName); return true;} catch (Exception e) {throw new UploadException (e.toString (), "FileUpload.uploadFile ()");}} public boolean uploadFile (String name) throws UploadException {// the Name of File Will Be Changd to Avoid Iteration String FileName = Name.Substring (Name.lastIndexof ("//") 1); Try {ftpclient.put (name, filename); return true;} catch (Exception E) { Throw New UploadException (E.TOString (), "FileUpload.uploadFile (Name)");}}} * * @ (#) DirectoryUpload.java 1.0 04/11/17 * * Upload folder * / package com.head. UPLOAD;

Import com.enterprisedt.net.ftp. *; import java.io. *;

Public Class DirectoryUpload Extends Upload {

DirectoryUpload () {} public boolean uploadFile (String dir) throws UploadException {// the name of directory will be changed to avoid iteration String dirName = dir.substring (dir.lastIndexOf ( "//") 1); ftpMkDir (dirName ); Try {string currentdir = ftpclient.pwd (); ftpclient.chdir; string [] subsfiles = new directory (DIR) .filenames (); for (int i = 0; i

Import com.enterprisedt.net.ftp. *; import java.io. *;

Public class directory {private string path; directory () {} Directory ()} {this.path = path;} // Return to all files in a directory String [] filenames () {string [] Subnames = null; try {File file = new file (path); subnames = file.list ();} catch (e) {system.out.println (e.tostring ());} returnis

/ * * @ (#) UPLOADEXCEPTION.JAVA 1.0 04/11/17 * * * / package com.Head.upload;

Public class uploadexception Excends Exception {UPLoadException (String Einfo, String location) {string info = "The error" local; system.out.println (info);}

}

Ok, it's so much. Where you want to change has already been indicated, I hope everyone has time to implement

转载请注明原文地址:https://www.9cbs.com/read-85732.html

New Post(0)