JSP file upload is easy to implement

Source: Internet
Author: User

Nowadays, jsp (the preferred choice for SUN enterprise-level applications) has been applied by more and more people to the Development of background programs and the production of dynamic web pages, today, we will introduce how to use jsp (the preferred choice for SUN Enterprise applications) to upload files.

After several days, I checked a lot of information on the Internet and finally solved the upload function. The procedure is as follows:

Package linkman. helper;

Import java. io .*;
Import java. util .*;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. ServletInputStream;

Public class UpLoadBean {
Private String SavePath = null;
Private String FileName = null;
Private String Sperator = null;
Private String ErrMsg = "Normal Return ";

Private byte [] bt = new byte [0, 4096];
Private byte [] prebt = new byte [1, 4096];
Private int t =-1;
Private long FileSize =-1;
Private boolean UserDefineFileName = false;

//////////////////////////////////////// /////////
Public UpLoadBean (){
FileName = bringFileName ();
}
Public UpLoadBean (String savepath ){
SavePath = savepath;
FileName = bringFileName ();
}

//////////////////////////////////////// /////////
// File Name Information
Private String bringFileName (){
Calendar dt = Calendar. getInstance ();
String str = "" + dt. get (Calendar. YEAR) + dt. get (Calendar. MONTH) + dt. get (Calendar. DAY_OF_MONTH );
Str = str + dt. get (Calendar. HOUR) + dt. get (Calendar. MINUTE) + dt. get (Calendar. SECOND );
Return str;
}

Public void setFileName (String filename ){
FileName = filename;
UserDefineFileName = true;
}
Public String getFileName (){
Return FileName;
}
//////////////////////////////////////// /////////
// Upload the file main program
Public boolean UpLoad (HttpServletRequest request) throws Exception {
If (SavePath = null ){
SetErrorMessage ("the path for saving the uploaded file is not set ");
Return false;
}
Request. setCharacterEncoding ("UTF-8 ");
ServletInputStream in = request. getInputStream ();
//////////////////////////////////////// ////
// Obtain the delimiter of a row
T = in. readLine (bt, 0, bt. length );
If (t! =-1 ){
Sperator = new String (bt, 0, t );
Sperator = Sperator. substring (0, 28 );
T =-1;
}

//////////////////////////////////////// ////
// Get the extension and set the full name of the file
Do {
T = in. readLine (bt, 0, bt. length );
String s = new String (bt, 0, t );
Int index = s. indexOf ("filename = "");
If (index! =-1 ){
S = s. substring (index + 10 );
Index = s. indexOf (""");
S = s. substring (0, index );
Index = s. lastIndexOf (".");
If (! UserDefineFileName ){
FileName = FileName + s. substring (index); // you can specify the full name of a file.
}
T =-1;
}
} While (t! =-1 );

//////////////////////////////////////// ////
// Obtain the File Content
FileOutputStream out = new FileOutputStream (SavePath + "\" + FileName );

T = in. readLine (bt, 0, bt. length );
String s = new String (bt, 0, t );
Int I = s. indexOf ("Content-Type :");
If (I =-1 ){
SetErrorMessage ("uploaded is not a file ");
Return false;
} Else {
In. readLine (bt, 0, bt. length); // remove an empty row.
T =-1;
}

Boolean state = true;
Long trancsize = 0;
Try {
T = in. readLine (bt, 0, bt. length );
While (t! =-1 ){
S = new String (bt, 0, t );
If (s. length ()> 28 ){
S = s. substring (0, 28 );
If (s. equals (Sperator )){
Break;
}
}

/*
If (bt [0] ==45 & bt [1] ==45 & bt [2] ==45 & bt [3] ==45 & bt [4] = 45) {
Break;
}
*/
If (FileSize! =-1 ){
If (trancsize> = FileSize ){
Throw new Exception ("uploading files too large ");
}
}
Out. write (bt, 0, t );
Trancsize + = t;
T = in. readLine (bt, 0, bt. length );
}
} Catch (Exception e ){
State = false;
Out. close ();
SetErrorMessage (e. getMessage ());
File tmp = new File (SavePath, FileName );
If (! Tmp. delete ()){
SetErrorMessage (e. getMessage () + ", clear unfinished ");
}
} Finally {
If (state ){

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.