幾種JSP上傳檔案代碼(1/4)

來源:互聯網
上載者:User

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 SinNeR@
* by vogoal.com
* mail: vogoals@hotmail.com
*/
/**
* 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 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.