Several JSP file upload codes (1/4)

Source: Internet
Author: User

JSP File Upload
Package com. vogoal. util;
Import java. io. BufferedOutputStream;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. ArrayList;
Import java. util. Date;
Import java. util. Hashtable;
Import javax. servlet. ServletInputStream;
Import javax. servlet. http. HttpServletRequest;
/*
* Vogoalapis 1.0
* Auther SinNeR@blueidea.com
* By vogoal.com
* Mail: vogoals@hotmail.com
*/
/**
* JSP File Upload
*
* @ Author SinNeR
* @ Version 1.0
*/
Public class JspFileUpload {
/** Request object */
Private HttpServletRequest request = null;
/** Upload File Path */
Private String uploadPath = null;
/** Get the size of the byte for each read */
Private static int BUFSIZE = 1024*8;
/** Hashtable */
Private Hashtable paramHt = new Hasp tutorial table ();
/** ArrayList */
Private ArrayList updFileArr = new ArrayList ();
/**
* Set the request object.
*
* @ Param request
* HttpServletRequest request object
*/
Public void setRequest (HttpServletRequest request ){
This. request = request;
}
/**
* Set the File Upload path.
*
* @ Param path
* File Upload path specified by the user.
*/
Public void setUploadPath (String path ){
This. uploadPath = path;
}
/**
* Main program for file upload processing. & #65533; & #65533; & #65533; & #65533; & #65533; & #65533; & #65533; B
*
* @ Return int operation result 0 file operation succeeded; 1 request object does not exist. 2. The file storage path is not set or the file storage path is incorrect. 3.
* An error occurred while setting the correct enctype; 4.
*/
Public int process (){
Int status = 0;
// Check the request object, upload path, and enctype before uploading the file.
Status = preCheck ();
// Return the error code when an error occurs.
If (status! = 0)
Return status;
Try {
// & #65533; & #65533; parameter or file name & #65533; u & #65533; & #65533;
String name = null;
// Parameter value
String value = null;
// Whether the read stream is the object flag
Boolean fileFlag = false;
// The file to be stored.
File tmpFile = null;
// Name of the uploaded file
String fName = null;
FileOutputStream baos = null;
BufferedOutputStream bos = null;
// & #65533; & #65533; Hashtable for storing Parameters
ParamHt = new Hashtable ();
UpdFileArr = new ArrayList ();
Int rtnPos = 0;
Byte [] buffs = new byte [BUFSIZE * 8];
// & Amp; #65533; get ContentType
String contentType = request. getContentType ();
Int index = contentType. indexOf ("boundary = ");
String boundary = "--" + contentType. substring (index + 9 );
String endBoundary = boundary + "--";
// & #65533; obtain the stream from the request object.
ServletInputStream sis = request. getInputStream ();
// Read 1 line
While (rtnPos = sis. readLine (buffs, 0, buffs. length ))! =-1 ){
String strBuff = new String (buffs, 0, rtnPos );
// Read 1 row of Data & #65533; n & #65533; & #65533;
If (strBuff. startsWith (boundary )){
If (name! = Null & name. trim (). length ()> 0 ){
If (fileFlag ){
Bos. flush ();
Baos. close ();
Bos. close ();
Baos = null;
Bos = null;
UpdFileArr. add (fName );
} Else {
Object obj = paramHt. get (name );
ArrayList al = new ArrayList ();
If (obj! = Null ){
Al = (ArrayList) obj;
}
Al. add (value );
System. out. println (value );
ParamHt. put (name, al );
}
}
Name = new String ();
Value = new String ();
FileFlag = false;
FName = new String ();
RtnPos = sis. readLine (buffs, 0, buffs. length );
If (rtnPos! =-1 ){
StrBuff = new String (buffs, 0, rtnPos );
If (strBuff. toLowerCase (). startsWith (
"Content-disposition: form-data ;")){
Int nIndex = strBuff. toLowerCase (). indexOf (
"Name = "");
Int nLastIndex = strBuff. toLowerCase (). indexOf (
", NIndex + 6 );
Name = strBuff. substring (nIndex + 6, nLastIndex );
} 1 2 3 4

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.