JavaBean is a Java-based software component. JSP provides a complete support for JavaBean for JavaBean-based software components in Web applications. JSP provides perfect support for integrating Javabean components in web applications. This support not only reduces development time (can directly utilize test and trusted components, avoid repeated development), but also bring more scalability for JSP applications. The upload function of the file is very common in B / S development model. Compared with other development tools, JSP's upload support for files is not perfect. It is neither a component that must be used as the ASP, and it is not as supported by file uploads like PHP. The implementation of the JSP implementation file upload is this: use the GetInputStream () method of the servletRequest class to get a client to send a data stream to the server, then process this data stream, analyze, get the file upload to pass to the server to each parameter and Data, then store the file data into a file or insert into the database. Usually the upload function of the file is not processed in the JSP page, but put these functions into the servlet or JavaBean. Examples uploaded using servlet have introduced in some JSP related books, I introduced how Jeanbean is uploaded using Jeanbean. The upload of the file in JSP can be implemented in two ways, namely HTTP protocol and FTP protocol implementation, and the two have great differences in the principle of transmission. The combined source code will be briefly introduced below, and believe that the reader will gain from it. The following procedures have been debugged. Debugging environment: Window 2000 Server Apache Tomcat4.0, JavaBean Debugging Environment: JDK1.4 Editplus. Using JSP Using JavaBean to implement Web-based file upload feature generally require three files to complete. These three files are the HTML page files that provide interfaces, and complete the JSP files for JavaBeans that implement the upload function and the Java's Java's class files. Here I will focus on the JavaBean section that implements the file upload function using the HTTP protocol and the FTP protocol. 1. Using the HTTP protocol to implement multiple files Upload in the past HTML, the form cannot be uploaded, which limits some of the functions of some web pages. RFC1867 specification (ie, implementing form-based file upload), an extended form is extended, adding a form element . By using this element, the browser automatically generates an input box and a button, the input box can fill in the local file name and path name, buttons allow the browser to open a file selection box for the user to select a file. The specific form is implemented as follows:
JavaBean obtains a client-side data stream to the server through the GetInputStream () method of the servletRequest class, analyzing the uploaded file format, and outputs multiple files in the target file of the server side based on the analysis results. The functionality of Javabeande in this example is implemented by the TestupLoad class. TestUpload frame following classes: public class testUpload {public testUpload () {......} public final void initialize (ServletConfig config) throws ServletException {m_application = config.getServletContext ();} public void upload () throws testUploadException, IOException, ServletException { .........} private void getDataSection () {.........} private void getDataHeader () {.........} public int save (String destPathName) throws SmartUploadException, IOException, ServletException {.........} ......} by initialize () method Initialize the service environment of the servlet. Use the UPLOAD () method to get the input stream, and analyze the format of the upload file, and assign the properties of each upload file to multiple File class instances, which are managed by Files class. The File class calls its Save () method according to the attributes of each file, sequentially outputs multiple files in the target file of the server. Where the UPLOAD () method is the key to analyze the format of the HTTP1.1 protocol transfer file. After testing, we got a format of transport stream files, which is useful to understand the UPLOAD () method. For example, upload my document /tt.txt file. The format is as follows: // File separator ----------------------------- 7D226137250336 // File Information Head Content-Disposition: Form-Data Name = "file1"; filename = "c: / documents and settings / administrator.timber-4o6b0zz0 / my documents / tt.sql" Content-type: text / plain // Source Create Table Info (Content Image Null) ; // Separator of the next file ---------------------------- 7D226137250336 Content-Disposition: form-data; name = " File2 "; filename =" "" Content-Type: Application / OcT-Stream ---------------------------- 7D226137250336 From the above document us It can be seen that when the HTTP protocol is uploading multiple files, it is to place all the files into the input stream and distinguish between a certain separator. In fact, the UPLOAD () method is to analyze the above file, determine the content of the separator, the content format of each file, the full path name of the file, and the beginning of the actual data of the file. The point herein is that the separator is random, which is all the characters before the first carrier of the stream file. The implementation process of the UPLOAD () method is: First output the input stream file to the byte array m_binarray, and implement it through the following code.
m_totalBytes = 1024; totalRead = 0; for (; totalRead The main achieve the following: public class ftpUpload {String filename; String filename1; FtpClient ftpClient; public void connectServer (string server, string user, string password, string path) {// server: IP address of the FTP server; user: login FTP server User Name // Password: The password of the username of the FTP server; PATH: The path on the FTP server Try {ftpclient = new ftpclient (); ftpclient.openserver (server); ftpclient.login (user, password); system.out .println ("Login Success!"); if (path.length ()! = 0) ftpclient.cd (path); ftpclient.binary ();} catch (ooException ex) {system.out.println (ex); }}} Public void closeconnect () {ftpclient.closserver ();} catch (ooException ex) {system.out.println (ex);}} public void upload ()}} public void upload ()}} public void upload ()}} public void upload ()}} public void upload ()}}}} public void upload ()}} The name of the file is analyzed in FileName. The name of the target file is the name of the target file. The specific method does not give TRY {TelnetOutputStream OS = ftpclient.put (filename1); java.io.file file_in = new java.io.file (filename); fileInputStream Is = new fileinputstream (file_in); byte [] bytes = new byte [1024]; int C; while ((c = is.read (bytes))! = - 1 ) {os.write (bytes, 0, c);} is.close ();} catch (ooException ex) {system.out.println (ex);}}} connectionserver () complete The FTP server establishes the function of the connection, open the remote FTP server using the ftpclient's OpenServer (String Server) method, then log in to the server using the FTPClient's Login (User, Password) method. There are two ways to log in to the remote FTP server, one is a registered user login, and the other is logged in anonymously. The former asks the user to register as a server, and the server will give the client a login account and password, and connect to the server based on the account number and password. The latter requires users to use special username "AnnoyMous" and "Guest" to access the remote host without registration, and now many systems require users to use the Email address as a password. For security purposes, most anonymous FTP hosts typically only allow remote users to download files, not uploading, which will depend on the setting of the FTP server. Users can choose two ways according to the actual situation. After the login is completed, the binary () method using ftpclient is initialized in the transmission mode as byte. Upload () completes the upload function of the file.