blog.Ring.idv.tw

2007 August

檔案上傳-「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 | Comments (2)

DoABC Tag格式似乎有誤~

上圖是SWF and FLV File Format Specification (Version 9)所描述的DoABC Tag,從圖上可以知道此Tag裡頭包含了「Flags」和「ABCData」,然而真正在剖析時,似乎並不是那麼一回事~

Flash CS3(AS3)

Flex Showcase 1(已經過解壓處理)

www.deanzaextreme.com

Flex Showcase 2(已經過解壓處理)

www.vinnitsa.com

上述三張圖的「紅色」部份是DoABC Tag所包含的「Header」、「Length」和「Flags」,而「藍色」部份則是AS3 bytecode的「minor_version」和「major_version」,重點在於「綠色」部份卻沒有任何文件的說明,然而從Flex Showcase 1,2卻可以發現他所包含的資訊卻是「frame1」的字元,所以此部份的格式型態應該屬於「String」,也就是說在「Flags」之後緊接著的是「frame編號的資訊」,最後才是「ABCData」,但這部份仍屬於筆者的猜測。

2007-08-03 17:19:53 | Add Comment

ActionScript Virtual Machine 2 (AVM2) Overview 文件誤植~

ActionScript Virtual Machine 2 (AVM2) Overview這份文件中的第66頁,描述此「greaterequals」instruction,但其底下的「Format」、「Forms」是有錯誤的~

如下所示:

錯誤

[Format]:greaterthan
[Forms]:greaterthan = 175 (0xaf)

更正

[Format]:greaterequals
[Forms]:greaterequals = 176 (0xb0)

參考文件:AVM2 instructions

2007-08-02 18:21:27 | Add Comment

How to create pop-up browser windows in Flash

How to create pop-up browser windows in Flash

這篇技術教學會教導你如何在瀏覽器內利用Flash來開啟視窗~

舉個例子來說,以往我們都會利用「getURL()」來達成這樣的功能,但在AS3.0就必須改用「navigateToURL()」,或是採用執行JavaScript的方式來達成,諸如此類的應用都可以在這篇教學中找到你的答案!

2007-08-01 14:34:06 | Add Comment

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

::: 搜尋 :::

::: 分類 :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment