JSP file upload source code, no restrictions on the upload size, first-class speed

Source: Internet
Author: User
<% @ Page contenttype = "text/html" %>
<% @ Page pageencoding = "gb2312" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Io. *" %>
<%
// JSP file upload source code. A single or multiple files can also be used in combination with other bidding types. The upload size is not limited and the speed is first-class.
// Author: sgzyl 2007.10.26
Request. setcharacterencoding ("GBK ");
Response. setcontenttype ("text/html; charset = gb2312 ");
Char [] hchlorophyll = {13, 10 };
String boundary = request. getcontenttype (). substring (30 );
String field_boundary = "--" + boundary + new string (hchlorophyll );
String last_boundary = "--" + boundary + "--" + new string (hchlorophyll );
Servletinputstream getdata = request. getinputstream ();
Bytearrayoutputstream temp = new bytearrayoutputstream ();
Byte [] data_line = new byte [1, 8192];
Int line_byte_count = 0;
Boolean found_boundary = false;
While (line_byte_count = getdata. Readline (data_line, 0, data_line.length ))! =-1 ){
If (! Found_boundary ){
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
}
String temp_str = new string (data_line, 0, line_byte_count );
If (temp_str.indexof ("FILENAME ")! =-1 ){
If (temp_str.substring (temp_str.indexof ("filename =") + 9, temp_str.lastindexof ("/" ") + 1). Length ()> 2 ){
String file_name = temp_str.substring (temp_str.lastindexof ("//") + 1, temp_str.lastindexof ("/""));
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Fileoutputstream myfile = new fileoutputstream ("D: //" + file_name, false );
Boolean test = true;
While (TEST ){
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
If (line_byte_count =-1 ){
Test = false;
Break;
}
If (temp. Size () = 0 ){
Temp. Write (data_line, 0, line_byte_count );
} Else {
If (new string (data_line, 0, line_byte_count). Equals (field_boundary) | new string (data_line, 0, line_byte_count). Equals (last_boundary )){
Myfile. Write (temp. tobytearray (), 0, temp. tobytearray (). Length-2 );
Temp. Reset ();
Myfile. Close ();
Out. println (file_name + "uploaded successfully <br> ");
Test = false;
Found_boundary = true;
} Else {
Temp. writeto (myfile );
Temp. Reset ();
Temp. Write (data_line, 0, line_byte_count );
}
}
}
} Else {
String field_name = temp_str.substring (temp_str.indexof ("name") + 6, temp_str.lastindexof (";")-1 );
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Found_boundary = true;
Out. println (field_name + "no file upload is selected! <Br> ");

}
} Else {
String field_name = temp_str.substring (temp_str.indexof ("name") + 6, temp_str.lastindexof ("/""));
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
Temp. Reset ();
Boolean test = true;
While (TEST ){
Line_byte_count = getdata. Readline (data_line, 0, data_line.length );
If (line_byte_count =-1 ){
Test = false;
Break;
}
If (new string (data_line, 0, line_byte_count). Equals (field_boundary) | new string (data_line, 0, line_byte_count). Equals (last_boundary )){
Test = false;
Found_boundary = true;
If (temp. Size ()> 2 ){
Out. println (field_name + ":" + new string (temp. tobytearray () + "<br> ");
} Else {
Out. println (field_name + "NO content! <Br> ");
}
Temp. Reset ();
} Else {
Temp. Write (data_line, 0, line_byte_count );
}
}
}

}
Getdata. Close ();
%>
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.