JSP upload File class
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;
/*
* VOGOALAPI 1.0
* Auther sinner@
* by vogoal.com
* mail:vogoals@hotmail.com
*/
/**
* JSP upload File class
*
* @author Sinner
* @version 1.0
*/
public class Jspfileupload {
/** Request Object * *
Private HttpServletRequest request = null;
/** upload the file path * *
Private String Uploadpath = null;
/** the size of bytes per read * *
private static int bufsize = 1024 * 8;
/** storage Parameters of Hashtable * *
Private Hashtable paramht = new hasp tutorial table ();
/** Store file name of uploaded file ArrayList * *
Private ArrayList Updfilearr = new ArrayList ();
/**
* Set the Request object.
*
* @param request
* HttpServletRequest Request Object
*/
public void Setrequest (HttpServletRequest request) {
This.request = Request;
}
/**
* Set file Upload path.
*
* @param path
* Upload path for user-specified files.
*/
public void Setuploadpath (String path) {
This.uploadpath = path;
}
/**
* File upload process main program. & #65533;& #65533;& #65533;& #65533;& #65533;& #65533;&; B
*
* @return int operation result 0 file operation succeeded; 1 Request object does not exist. 2 no Set file save path or file save path is incorrect; 3
* The correct enctype;4 file operation exception was not set.
*/
public int process () {
int status = 0;
Check the Request object, upload path, and enctype before uploading the file.
Status = PreCheck ();
Returns an error code when an error occurs.
if (status!= 0)
return status;
try {
& #65533;& #65533 parameter or filename & #65533;u& #65533;& #65533;
String name = NULL;
Value of the parameter
String value = null;
Whether the stream being read is a flag bit of the file
Boolean fileflag = false;
The file to store.
File tmpfile = null;
The 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];
& #65533; Get contenttype
String ContentType = Request.getcontenttype ();
int index = Contenttype.indexof ("boundary=");
String boundary = "--" + contenttype.substring (index + 9);
String endboundary = boundary + "--";
& #65533 to get the stream from the request object.
ServletInputStream sis = Request.getinputstream ();
Read 1 rows
while ((Rtnpos = sis.readline (buffs, 0, Buffs.length))!=-1) {
String strbuff = new String (buffs, 0, Rtnpos);
Read 1 rows 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);
}