Two methods to realize multi-file uploading JavaBean

Source: Internet
Author: User
Tags config file upload ftp html page requires ftp protocol

Absrtact: This paper introduces two methods of JavaBean to realize multiple file uploads, which are implemented using HTTP protocol and FTP protocol respectively. First, the HTTP protocol to transfer the basic format of multiple files and upload the detailed process, and then briefly introduced the use of FtpClient class to achieve the FTP upload, and finally the two methods are compared.

JavaBean is a Java-based software component. JSP provides complete support for integrating JavaBean components in Web applications. This support not only shortens development time (can directly leverage tested and trusted existing components, avoids duplication of development), but also provides more scalability for JSP applications.

File upload function is very common in the development mode based on B/S. Compared with other development tools, JSP upload support to the file is not perfect, it is not as ASP as the need to use components to complete, and not as PHP as the direct provision of file upload support. The implementation of the JSP implementation file upload is this: using the ServletRequest class of the getInputStream () method to obtain a client sent to the server data flow, and then process the data flow, from the analysis, to get the file on the cross to the server parameters and data, The file data is then stored as a file or inserted into the database. In general, JSP pages do not process the upload function of the file, but put these functions into the servlet or JavaBean to achieve. Using the servlet to complete the file upload examples in some JSP related books are introduced, I introduce here to use Jeanbean is how to complete the file upload. The implementation of file upload in JSP can be implemented by HTTP protocol and FTP protocol in two ways, and there are great differences in the principle of transmission. The following will be combined with the source code to do a brief introduction to their implementation, I believe that readers will gain. The following programs have been debugged. Debugging Environment: Window Server+apache +tomcat4.0,javabean Debugging Environment: Jdk1.4+editplus.

The use of JavaBean in JSP to implement the Web-based file upload function generally requires three kinds of files combined to complete. These three kinds of files are the HTML page files that provide the interface, the JavaBean JSP file that implements the upload function, and the Java class file that implements the JavaBean. Below I will focus on the use of HTTP protocol and FTP protocol to achieve file upload function of the JavaBean part.

1 using HTTP protocol to upload multiple files

In the past HTML, the form does not implement file upload, which limits some of the functionality of some Web pages. The RFC1867 specification, which implements the form-based file upload in HTML, expands the form to add a table cell element 〈input type=file>. By using this element, the browser automatically generates an input box and a button that allows the user to fill in the local file name and path name, and the button lets the browser open a File selection box for the user to select the file. The specific form implementation is as follows:

<FORMMETHOD="POST" ACTION="*.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload">
</FORM>

When you select the Paste file to enter the absolute path of the local file directly, the form's Action property value is *.jsp, which means that the request (including the uploaded file) will be sent to * ... JSP files. In this process, a file upload is actually implemented in HTTP mode. The upload of files from client to server is supported by the Common Gateway Interface (CGI) of the HTTP protocol. This upload mode requires both the browser and webserver to support Rfc1867. JavaBean through the ServletRequest class of getInputStream () method to obtain a client sent to the server data flow, analysis uploaded file format, according to the results of the analysis of multiple files to output server-side target files. The function of the Javabeande in this example is implemented by the Testupload class. The framework of the Testupload class is as follows:

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
{………}
……
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.