程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 幾種JSP上傳文件代碼(1/4)

幾種JSP上傳文件代碼(1/4)

編輯:關於JSP

JSP上傳文件類
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 [email protected]
* by vogoal.com
* mail: [email protected]
*/
/**
* JSP上傳文件類
*
* @author SinNeR
* @version 1.0
*/
public class JspFileUpload {
    /** request對象 */
    private HttpServletRequest request = null;
    /** 上傳文件的路徑 */
    private String uploadPath = null;
    /** 每次讀取得字節的大小 */
    private static int BUFSIZE = 1024 * 8;
    /** 存儲參數的Hashtable */
    private Hashtable paramHt = new Hasp教程table();
    /** 存儲上傳的文件的文件名的ArrayList */
    private ArrayList updFileArr = new ArrayList();
    /**
     * 設定request對象。
     *
     * @param request
     *            HttpServletRequest request對象
     */
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    /**
     * 設定文件上傳路徑。
     *
     * @param path
     *            用戶指定的文件的上傳路徑。
     */
    public void setUploadPath(String path) {
        this.uploadPath = path;
    }
    /**
     * 文件上傳處理主程序。�������B
     *
     * @return int 操作結果 0 文件操作成功;1 request對象不存在。 2 沒有設定文件保存路徑或者文件保存路徑不正確;3
     *         沒有設定正確的enctype;4 文件操作異常。
     */
    public int process() {
        int status = 0;
        // 文件上傳前,對request對象,上傳路徑以及enctype進行check。
        status = preCheck();
        // 出錯的時候返回錯誤代碼。
        if (status != 0)
            return status;
        try {
            // ��參數或者文件名�u��
            String name = null;
            // 參數的value
            String value = null;
            // 讀取的流是否為文件的標志位
            boolean fileFlag = false;
            // 要存儲的文件。
            File tmpFile = null;
            // 上傳的文件的名字
            String fName = null;
            FileOutputStream baos = null;
            BufferedOutputStream bos = null;
            // ��存儲參數的Hashtable
            paramHt = new Hashtable();
            updFileArr = new ArrayList();
            int rtnPos = 0;
            byte[] buffs = new byte[BUFSIZE * 8];
            // �取得ContentType
            String contentType = request.getContentType();
            int index = contentType.indexOf("boundary=");
            String boundary = "--" + contentType.substring(index + 9);
            String endBoundary = boundary + "--";
            // �從request對象中取得流。
            ServletInputStream sis = request.getInputStream();
            // 讀取1行
            while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
                String strBuff = new String(buffs, 0, rtnPos);
                // 讀取1行數據�n��
                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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved