Form enctype = multipart/form-Data

Source: Internet
Author: User
Tags file url

This day, I wrote a jsppage table to submit the excel.xls document. At the same time, there are some HTML field variables on the page. We found that the form encoding is set to enctype = "multipart/form-data"

Request. getparameter ("? "); The value of the page field cannot be obtained. Later, we found that enctype =" multipart/form-Data "in the form indicates binary encoding and the value of the page field cannot be obtained through the request directly, however, variable values can be obtained through workarounds. The internal principle is binary conversion. Some open-source Java components perform secondary packaging on requests to obtain the value of the page field.

 

The default form encoding format is application/X-WWW-form-urlencoded, which cannot be used for file upload. Only multipart/form-data can be used to completely transmit file data, perform the following operations.
Enctype =/"multipart/form-data/" is used to upload binary data. The input values in form are passed in binary format.
The input value in form is passed in binary format, so the request will not get the value.

 

Currently, the following components are uploaded and downloaded in JSP:

1. smartupload component

Smartupload is a free-of-charge, full-featured file upload and download component developed by www.jspsmart.com. It is suitable for embedding JSP files for uploading and downloading operations. This component has the following features:
1. Easy to use. You can easily upload or download files by writing only three or five lines of Java code in JSP files.
2. Full upload control. The objects provided by the jspsmartupload component and their operation methods can be used to obtain information about all uploaded files (including file names, sizes, types, extension names, and file data) for easy access.
3. restrict the size and type of uploaded files. This filters out non-conforming files.
4. Flexible download. Only two lines of code can be written to convert the Web server into a file server. You can use smartupload to download files in the web server directory or any other directory.
5. You can upload files to the database or download data from the database. This function is applicable to MySQL databases and is not universal.

 

Ii. commons-fileupload component

The common-fileupload component is one of Apache's open-source projects. It can be downloaded from http://jakarta.apache.org/commons/fileupload. This component is easy to use and allows you to upload one or more files at a time and limit the file size.
Download and unzip the zip package, copy the commons-fileupload-1.1.1.jar, and commons-io-1.2.jar to Tomcat's webapps/Your webapp/WEB-INF/lib/, if the directory does not exist please build your own directory.

 

Iii. uploadbean Components

UploadbeanJava component is a component used for file upload. You can save the uploaded files in the server folder or package them into a zip file and save them in the folder, you can also save files in the database or memory. This document is intended for developers. We assume that you have learned about some components, such as JavaBeans and JSP technologies, and know what components are used for "Scope" (page, session, and application ).

First, you need to select the storage mode: [Folder] [ZIP Archive] [database] [Memory]. Then you can switch through the [setstoremodel] method. Next, you can add some restrictions, such as [blacklist], [whitelist], and [maximum. To store uploaded files, you must call the [store] Method of the [multipartformdatarequest] object. (The [overwrite] option is used to process duplicate upload files ). Finally, you can [Reset] the bucket if necessary. When a file is uploaded, the class implementing the [uploadlistener] interface can receive file upload events. The information of the uploaded file is stored in [history] (non-binary file ). Uploadbean supports two types of Resolvers. You can set them using the [parser] attribute.

 

Uploadbean can upload files to any directory or folder on the server. The servlet engine must have read and write permissions on the folder and directory.

Set the folder storage mode:

Public voidSetfolderstore(String serverfolder)
Parameter: full path of the folder (e.g./usr/uploads ).
If the folder does not exist, an empty folder is created.

Uploadbean can save files in a zip file. The servlets engine must have read and write permissions on the folder containing the ZIP file.

 

Public voidSetzipfilestore(String file)
Parameter: full path of the ZIP file (e.g./usr/upload/uploaded.zip ).
If no zip file exists, an empty ZIP file is created.

Uploadbean can store uploaded files in any long raw data (such as binary files) that supports long raw data ). You need a table (uploads) with three fields: uploadid (Primary Key), filename (Upload File name), and binary data (binaryfile ). You can use the sqlupload * Public static member variable of the uploadbean class to change the table and field name.

Initialize the JDBC connection:
Public voidSetdatabasestore(String driver, string URL, properties credentials)
Parameter: JDBC driver ID, jdbc url, and other connection attributes (User Name/password ).
This method loads the driver into the memory and establishes a database connection.

Public voidSetdatabasestore(Connection jdbcconnection)
Parameter: database connection name. The connection can come from the data source or connection pool.

 

Uploadbean can store uploaded files in JVM memory. Uploaded files will be stored in a "vector" object. This is a vector of the "uploadfile" type. (For more information, see the API ).
Set the memory storage mode:

Public voidSetstoremodel(INT storeid)
Parameter: memory storage identifier (memorystore

 

 

Switch the storage mode.

Public voidSetstoremodel(INT storeid)
Parameter: storage identifier...

 

  • Project homepage:
  • Document address: http://www.javazoom.net/jzservlets/uploadbean/documentation_cn/developerguide.html
  • : Http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
  •  

     

    4.Download4jComponents

    Download4j is a Java component (servlet + JavaBean) that allows downloading files from folders as the source,
    A zip archive or database (in Oracle, DB2, MySQL...). To set download4j, you need to write or customize the xml configuration file.
    Each source has a configuration file: [Folder], [ZIP Archive], and [database]. You can define global and specific file downloads, attributes, such as counters, Logon/password,
    Finally, you must declare this configuration file and map the downloaded URL to the download4j deployment descriptor,
    Then the end user can download the obtained File URL, the download trace, such as the date and time, and the end user's IP address.

     

    Download4j can download directories or files stored in any folder {
    _ Tipon (this)
    } "Onmouseout =" function onmouseout ()
    {
    _ TIPOFF ()
    } "> The folder and file must have the read permission.
    {
    _ Tipon (this)
    } "Onmouseout =" function onmouseout ()
    {
    _ TIPOFF ()
    } "> Here is the download4j configuration of a file sample:

    {
    _ Tipon (this)
    } "Onmouseout =" function onmouseout ()
    {
    _ TIPOFF ()
    } ">

    XML sample for folder Source:Folder. xml
    <Download>
    <Folder Id = "uniqueid" Path = "">
    <Properties Content-Type-file = "CONF/mimes-type.xml"/>
    <File name = "protectedtest.pdf" maxdownload = "3" Login = "login" Password = "password"/>
    <File name = "ziptest.html" urlmapping = "ziptest.zip" Zip = "enabled"/>
    </Folder>
    </Download>

     

     

     

     

     

     

     

     

     

    Document address: http://www.javazoom.net/jzservlets/download4j/documentation/configurationguide.html

     

    The efficiency of these components requires further research ...............

    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.