SoFunction
Updated on 2025-03-10

Jsp page implements file upload and download code page 1/2

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/*
* vogoalAPI 1.0
* Auther SinNeR@
* by
* mail: vogoals@
*/
/**
* JSP upload file class
*
* @author SinNeR
* @version 1.0
*/
public class JspFileUpload {
/** request object */
    private HttpServletRequest request = null;
/** The path to upload the file */
    private String uploadPath = null;
/** The size of bytes is obtained per read */
    private static int BUFSIZE = 1024 * 8;
/** Hashtable that stores parameters */
    private Hashtable paramHt = new Hasptable();
/** ArrayList that stores the file name of the uploaded file */
    private ArrayList updFileArr = new ArrayList();
    /**
* Set the request object.
     *
     * @param request
*           HttpServletRequest request object
     */
    public void setRequest(HttpServletRequest request) {
         = request;
    }
    /**
* Set the file upload path.
     *
     * @param path
*                 Upload path of the file specified by the user.
     */
    public void setUploadPath(String path) {
         = path;
    }
    /**
* File upload processing main program. ������B
     *
* @return int Operation result 0 File operation was successful; 1 request object does not exist. 2 The file saving path is not set or the file saving path is incorrect; 3
*            The correct enctype is not set; 4 File operation is abnormal.
     */
    public int process() {
        int status = 0;
// Before uploading the file, check the request object, upload path and enctype.
        status = preCheck();
// Return the error code when an error occurs.
        if (status != 0)
            return status;
        try {
// ��� parameter or file name�u�
            String name = null;
// Value of parameter
            String value = null;
// Whether the read stream is the file's flag bit
            boolean fileFlag = false;
// File to be stored.
            File tmpFile = null;
// The name of the uploaded file
            String fName = null;
            FileOutputStream baos = null;
            BufferedOutputStream bos = null;
// Hashtable for storing parameters
            paramHt = new Hashtable();
            updFileArr = new ArrayList();
            int rtnPos = 0;
            byte[] buffs = new byte[BUFSIZE * 8];
// � Obtain ContentType
            String contentType = ();
            int index = ("boundary=");
            String boundary = "--" + (index + 9);
            String endBoundary = boundary + "--";
// � Get the stream from the request object.
            ServletInputStream sis = ();
// Read 1 line
            while ((rtnPos = (buffs, 0, )) != -1) {
                String strBuff = new String(buffs, 0, rtnPos);
// Read 1 line of data
                if ((boundary)) {
                    if (name != null && ().length() > 0) {
                        if (fileFlag) {
                            ();
                            ();
                            ();
                            baos = null;
                            bos = null;
                            (fName);
                        } else {
                            Object obj = (name);
                            ArrayList al = new ArrayList();
                            if (obj != null) {
                                al = (ArrayList) obj;
                            }
                            (value);
                            (value);
                            (name, al);
                        }
                    }
                    name = new String();
                    value = new String();
                    fileFlag = false;
                    fName = new String();
                    rtnPos = (buffs, 0, );
                    if (rtnPos != -1) {
                        strBuff = new String(buffs, 0, rtnPos);
                        if (().startsWith(
                                "content-disposition: form-data; ")) {
                            int nIndex = ().indexOf(
                                    "name=\"");
                            int nLastIndex = ().indexOf(
                                    "\"", nIndex + 6);
                            name = (nIndex + 6, nLastIndex);
                        }
                        int fIndex = ().indexOf(
                                "filename=\"");
                        if (fIndex != -1) {
                            fileFlag = true;
                            int fLastIndex = ().indexOf(
                                    "\"", fIndex + 10);
                            fName = (fIndex + 10, fLastIndex);
                            fName = getFileName(fName);
                            if (fName == null || ().length() == 0) {
                                fileFlag = false;
                                (buffs, 0, );
                                (buffs, 0, );
                                (buffs, 0, );
                                continue;
                            }else{
                                fName = getFileNameByTime(fName);
                                (buffs, 0, );
                                (buffs, 0, );
                            }
                        }
                    }
                } else if ((endBoundary)) {
                    if (name != null && ().length() > 0) {
                        if (fileFlag) {
                            ();
                            ();
                            ();
                            baos = null;
                            bos = null;
                            (fName);
                        } else {
                            Object obj = (name);
                            ArrayList al = new ArrayList();
                            if (obj != null) {
                                al = (ArrayList) obj;
                            }
                            (value);
                            (name, al);
                        }
                    }
                } else {
                    if (fileFlag) {
                        if (baos == null && bos == null) {
                            tmpFile = new File(uploadPath + fName);
                            baos = new FileOutputStream(tmpFile);
                            bos = new BufferedOutputStream(baos);
                        }
                        (buffs, 0, rtnPos);
                        ();
                    } else {
                        ("test :" + value + "--" + strBuff);
                        value = value + strBuff;
                    }
                }
            }
        } catch (IOException e) {
            status = 4;
        }
        return status;
    }
    private int preCheck() {
        int errCode = 0;
        if ( request == null )
            return 1;
        if ( uploadPath == null || ().length() == 0 )
            return 2;
        else{
            File tmpF = new File(uploadPath);
            if (!())
                return 2;
        }
        String contentType = ();
        if ( ("multipart/form-data") == -1 )
            return 3;
        return errCode;
    }
    public String getParameter(String name){
        String value = "";
        if ( name == null || ().length() == 0 )
            return value;
        value = ((name) == null)?"":(String)((ArrayList)(name)).get(0);
        return value;
    }
    public String[] getParameters(String name){
        if ( name == null || ().length() == 0 )
            return null;
        if ( (name) == null )
            return null;
        ArrayList al = (ArrayList)(name);
        String[] strArr = new String[()];
        for ( int i=0;i<();i++ )
            strArr[i] = (String)(i);
        return strArr;
    }

    public int getUpdFileSize(){
        return ();
    }

    public String[] getUpdFileNames(){
        String[] strArr = new String[()];
        for ( int i=0;i<();i++ )
            strArr[i] = (String)(i);
        return strArr;
    }
    private String getFileName(String input){
        int fIndex = ("\\");
        if (fIndex == -1) {
            fIndex = ("/");
            if (fIndex == -1) {
                return input;
            }
        }
        input = (fIndex + 1);
        return input;
    }
    private String getFileNameByTime(String input){
        int index = (".");
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return (0,index) + (dt) + (index);
    }
}