[Asp, JSP, Asp.net] File Upload Technology ~~

Source: Internet
Author: User

File Upload is a meaningful topic.
I will discuss its principles, practical applications, and security design ..

When a form is set to enctype = "multipart/form-Data" method = "Post, if the <input type = "file" name = "FILENAME"/> label is selected by the user.

The browser will format the file content and all form fields and then pass them to the server ~~

As follows:
One test:

<! -- F. jsp -->
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Body>
<P> select the file to be uploaded: <br>
<Form action = "Accept. jsp" method = "Post" enctype = "multipart/form-Data">
<Input type = file name = "boy" size = "38">
<Br>
<Input type = "hidden" id = "TT" name = "T" value = "1">
<Input type = "Submit" id = "GG" name = "G" value = "Submit">
</Body>
</Html>

Processing:
[Color = Green]
<! -- Accept. jsp -->
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Body>
<% Try {inputstream in = request. getinputstream ();
File F = new file ("F: // Qin // jsp", "a.txt ");
Fileoutputstream o = new fileoutputstream (f );
Byte B [] = new byte [1024];
Int N;
While (n = in. Read (B ))! =-1)
{O. Write (B, 0, N );
}
O. Close ();
In. Close ();

}
Catch (ioexception ee ){}
Out. Print ("File Uploaded ");
%>
</Body>
</Html>
[/Color]

Read all the bytes from the request stream in accept. jsp and save them as text files,
After submission, view F:/Qin/a.txt as follows:

----------------------------- 7d631c72a0204
Content-Disposition: Form-data; name = "boy"; filename = "C:/Documents and Settings/goodday/My Documents/2005sn.txt"
Content-Type: text/plain

Installation Method

In "add Delete", select Delete "Visual Studio 2005", select the last "upgrade to official version" in the window that appears, and enter the following CD-key, click upgrade!

SN: KYTYH-TQKW6-VWPBQ-DKC8F-HWC4J
----------------------------- 7d631c72a0204
Content-Disposition: Form-data; name = "T"

1
----------------------------- 7d631c72a0204
Content-Disposition: Form-data; name = "G"

Submit
----------------------------- 7d631c72a0204 --

------------------------------- 7d631c72a0204 is the delimiter between fields;
----------------------------- 7d631c72a0204 -- end character.
The Delimiter is 42 bytes,
The first 29-, followed by the browser ID, ie is 7d, followed by 11 random hexadecimal one-digit characters.
Getparameter (JSP) or Form (ASP) cannot be used after the request stream is obtained in a binary format)
To read the value of the parameter.
We need to rewrite the analysis method to obtain the parameter value.

The format of the name and value of each parameter in the form field is:

Content-Disposition: Form-data; name = "T"

1

Name = "T" is the name, and the value is after two/R/N (newline)

The file contains one more filename = and one line of Content-Type: text/plain.
Then the same two newlines are followed by all the content of the binary file.
Until the separator...

------------------------

After analyzing these principles, we can implement the file upload function ~~~
The request stream is read by the delimiter;
Analyze the name and value of each segment.
Save to hashtable;

If it is a file, it is saved to the file.
-----------------------------------

 

Example of JSP:

File Upload class: uploadbean. Java
Package Qin;

Import java. Io .*;

Import java. util .*;

Import javax. servlet .*;

Import javax. servlet. http .*;

Public class uploadbean {

Private string [] sourcefile = new string [255]; // source file name

Private string [] suffix = new string [255]; // file suffix

Private string cansuffix = ".gif.jpg.jpeg.png.rar.txt"; // Suffix of the file to be uploaded

Private string objectpath = "F:/"; // target file directory

Private string [] objectfilename = new string [255]; // target file name

Private servletinputstream SIS = NULL; // input stream

Private string [] Description = new string [255]; // describes the status.

Private long size = 100*1024; // The maximum size

Private int COUNT = 0; // Number of transmitted files

Private byte [] B = new byte [4096]; // byte stream storage array

Private Boolean successful = true;

Private hashtable fields = new hashtable ();

Public uploadbean (){

}

// Set the suffix of the uploaded file

Public void setsuffix (string cansuffix ){

This. cansuffix = cansuffix;

}

// Set the file storage path

Public void setobjectpath (string objectpath ){

This. objectpath = objectpath;

}

// Set the file storage path

Public void setsize (long maxsize ){

This. size = maxsize;

}

// File Upload Handler

Public void setsourcefile (httpservletrequest request) throws ioexception {

Sis = request. getinputstream ();

Int A = 0;

Int K = 0;

String S = "";

While (A = sis. Readline (B, 0, B. Length ))! =-1 ){

S = new string (B, 0, );

If (k = S. indexof ("filename = /""))! =-1 ){

// Obtain file data

S = S. substring (K + 10 );

K = S. indexof ("/"");

S = S. substring (0, k );

Sourcefile [count] = s;

K = S. lastindexof (".");

Suffix [count] = S. substring (k + 1 );

If (cantransfer (count )){

Transferfile (count );

}

++ Count;

} Else if (k = S. indexof ("name = /""))! =-1 ){

// Normal form cannot be changed? Nbsp;

String fieldname = S. substring (K + 6, S. Length ()-3 );

Sis. Readline (B, 0, B. Length );

Stringbuffer fieldvalue = new stringbuffer (B. Length );

While (A = sis. Readline (B, 0, B. Length ))! =-1 ){

S = new string (B, 0, A-2 );

If (B [0] = 45) & (B [1] = 45) & (B [2] = 45) & (B [3] = 45) & (B [4] = 45 )){

Break;

} Else {

Fieldvalue. append (s );

}

}

Fields. Put (fieldname, fieldvalue. tostring ());

}

If (! Successful)

Break;

}

}

// Obtain the form Element value

Public String getfieldvalue (string fieldname ){

If (fields = NULL ****** fieldname = NULL ){

Return NULL;

}

Return (string) fields. Get (fieldname );

}

// Obtain the number of uploaded files

Public int getcount (){

Return count;

}

// Obtain the target path

Public String getobjectpath (){

Return objectpath;

}

// Obtain the source file name

Public String [] getsourcefile (){

Return sourcefile;

}

// Obtain the target file name

Public String [] getobjectfilename (){

Return objectfilename;

}

// Obtain the upload status description

Public String [] getdescription (){

Return description;

}

// Determine the type of the uploaded file

Private Boolean cantransfer (int I ){

Suffix [I] = suffix [I]. tolowercase ();

// This is used to upload images. You can change the suffix or not use this condition.

If (sourcefile [I]. Equals ("")******(! (Cansuffix. indexof ("." + suffix [I])> = 0 ))){

Description [I] = "Err: file suffix is wrong .";

Return false;

}

Else {

Return true;

}

}

// Upload file conversion

Private void transferfile (int I ){

String x = long. tostring (New java. util. Date (). gettime ());

Try {

Objectfilename [I] = x + "." + suffix [I];

Fileoutputstream out = new fileoutputstream (objectpath + objectfilename [I]);

Int A = 0;

Int K = 0;

Long hastransfered = 0; // indicates the number of transmitted bytes.

String S = "";

While (A = sis. Readline (B, 0, B. Length ))! =-1 ){

S = new string (B, 0, );

If (k = S. indexof ("Content-Type :"))! =-1 ){

Break;

}

}

Sis. Readline (B, 0, B. Length );

While (A = sis. Readline (B, 0, B. Length ))! =-1 ){

S = new string (B, 0, );

If (B [0] = 45) & (B [1] = 45) & (B [2] = 45) & (B [3] = 45) & (B [4] = 45 )){

Break;

}

Out. Write (B, 0, );

Hastransfered + =;

If (hastransfered> = size ){

Description [count] = "Err: the file" + sourcefile [count] +

"Is too large to transfer. The whole process is interrupted .";

Successful = false;

Break;

}

}

If (successful ){

Description [count] = "right: the file" + sourcefile [count] +

"Has been transfered successfully .";

}

Out. Close ();

If (! Successful ){

Sis. Close ();

File TMP = new file (objectpath + objectfilename [count]);

TMP. Delete ();

}

}

Catch (ioexception IOE ){

Description [I] = IOE. tostring ();

}

}

Public static void main (string [] ARGs ){

System. Out. println ("test OK ");

}

}

File Upload call: Upload. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>

<HTML>

<Head>

<Title> File Upload </title>

</Head>

<Body>

<Form action = "uploadsubmit. jsp" enctype = "multipart/form-Data" method = "Post">

Author name: <input type = "text" name = "author"/>

<Br/>

Company Name: <input type = "text" name = "company"/>

<Br/>

File Description: <input type = "text" name = "comment"/>

<Br/>

Select File 1: <input type = "file" name = "filename1"/>

<Br/>

Select File 2: <input type = "file" name = "filename2"/>

<Br/>

Select File 3: <input type = "file" name = "filename3"/>

<Br/>

Select File 4: <input type = "file" name = "filename4"/>

<Br/>

<Input type = "Submit" value = "Upload"/>

</Form>

</Body>

</Html>

File Upload and submission: uploadsubmit. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>

<JSP: usebean id = "filebean" Scope = "page" class = "Qin. uploadbean"/>

<%

Filebean. setobjectpath ("D ://");

Filebean. setsize (10000*1024 );

Filebean. setsuffix (".gif.jpg.png.jpg.html.htm ");

Filebean. setsourcefile (request );

String [] sasourcefile = filebean. getsourcefile ();

String [] saobjectfile = filebean. getobjectfilename ();

String [] sadeworkflow = filebean. getdescription ();

Int icount = filebean. getcount ();

String sobjectpath = filebean. getobjectpath ();

For (INT I = 0; I <icount; I ++ ){

Out. println ("<br> source start file :");

Out. println (sasourcefile [I]);

Out. println ("<br> target file :");

Out. println (sobjectpath + saobjectfile [I]);

Out. println ("<br> upload Description :");

Out. println (sadeworkflow [I]);

Out. println ("<br> ");

}

Out. println ("<br> author:" + filebean. getfieldvalue ("author "));

Out. println ("<br> company:" + filebean. getfieldvalue ("company "));

Out. println ("<br> Description:" + filebean. getfieldvalue ("comment "));

%>

 

Related Article

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.