blog.Ring.idv.tw

檔案上傳-「commons fileUpload」

檔案上傳-「commons fileUpload」


一般我們常見的HTML輸入型態(例如:text、radio、select…)都是使用「application/x-www-form-urlencoded」的編碼方式,但要傳送檔案至伺服端時,編碼方式則是要仰賴「multipart/form-data」,由於兩者的編碼方式不同,所以這裡提供一個「FileUpload」的小範例~

必要的package如下:

commons fileUpload

commons IO

HTML

<%@page language="java" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>檔案上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<b>檔案上傳</b></font></p>
<form name="UploadForm" enctype="multipart/form-data" method="post" action="fileupload.dan">
    <input type="file" name="File1" size="20" maxlength="20"> <br>
    <input type="text" name="File2" size="20" maxlength="20"> <br>
    <input type="submit"value="上傳">
</form>
</body>
</html>

Servlet

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet
{
    private String base;
    
    public void init(ServletConfig sc) throws ServletException
    {
        ServletContext sco = sc.getServletContext();
        base = sco.getRealPath("/");
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {   
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        try
        {
            if(isMultipart)
            {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                factory.setSizeThreshold(4096);
                factory.setRepository(new File(base+"temp"));
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setSizeMax(10000000);
                List items = upload.parseRequest(req);
                Iterator iter = items.iterator();
                while(iter.hasNext())
                {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField())
                    {
                        String name = item.getFieldName();
                        String value = item.getString();
                        System.out.println("name:"+name+" value:"+value);
                    } else {
                        String fieldName = item.getFieldName();
                        String fileName = item.getName();
                        String contentType = item.getContentType();
                        boolean isInMemory = item.isInMemory();
                        long sizeInBytes = item.getSize();
                        System.out.println("fieldName:"+fieldName+" fileName:"+fileName);
                        File to = new File(base+"upload",fileName);
                        item.write(to);
                    }
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

2007-08-10 14:52:00

2 comments on "檔案上傳-「commons fileUpload」"

  1. 1. nick 說:

    站長你好啊..
    這個範例不錯用..
    但請問有沒有辦法在html裡...
    加入一次可以選多個檔案的標籤...
    這樣servlet就可以連續處理多個檔案了???

    2008-06-19 17:38:19

  2. 2. Shen 說:

    謝謝,你指的是多檔上傳嗎?
    網路上有許多類似的相關範例~
    http://www.blogjava.net/sitinspring/archive/2008/04/12/192408.html
    參考看看~

    2008-06-19 18:37:20

Leave a Comment

Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment

::: 人氣指數 :::

今日人氣:459

累積人氣:3006741


::: 線上人數 :::

counter