JSP file upload source code, no restrictions on the upload size, first-class speed
Last Update:2018-12-05
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 ();
%>