日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

透過portlet 上傳檔案(適用portal 4.2 api)

 smoking_boy 2005-08-25
在這個例子中我們使用的平臺為IBM WebSphere Portal 4.2,開發(fā)工具為WSAD + Portal Toolkit 4.7,檔案上傳套件為jakarta commons專案中的FileUpload套件。FileUpload套件下載網(wǎng)址為http://jakarta./commons/fileupload/。下載檔案為commons-fileupload-1.0.zip,解壓縮後會發(fā)現(xiàn)內(nèi)含一個commons-fileupload-1.0.jar檔案,這就是我們需要的檔案上傳類別庫。接下來一步步建立一個Portlet Application。
(1)新建一個Portlet Application,名稱為FileUpload。
(2)將先前所提的commons-fileupload-1.0.jar類別庫檔案加入Portlet Application的類別庫目錄中({$WSAD_WORKSPACE}/FileUpload/WebContent/WEB-INF/lib/)
(3)在FileUpload中加入一個新的Portlet,名稱為FileUploadPortlet
(4)在FileUploadPortlet中先建立第一個View Fragment,名稱為Initial.jsp。在Initial.jsp建立一個表單,表單內(nèi)包含上傳檔案的欄位。參考程式碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page contentType="text/html; charset=Big5"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>
<portletAPI:init/>
 
<form name="frmFileUpload" 
         method="POST" 
         enctype="multipart/form-data" 
         action="<portletAPI:createURI><portletAPI:URIAction name="upload" /></portletAPI:createURI>">
<table>
  <tr>
    <td valign="middle" align="right">請輸入檔案描述:</td>
    <td><input name="desc" type="text" size="20"></td>
  </tr>
  <tr>
    <td valign="middle" align="right">請選擇欲上傳的檔案:</td>
    <td><input name="file" type="file" size="20"></td>
  </tr>
</table>
<input type="submit" value="確定">
</form>

要注意的地方是,如果是包含上傳檔案欄位的表單,則FORM的ENCTYPE屬性要設(shè)為multipart/form-data
(5)實作Portlet內(nèi)容,也就是FileUploadPortlet.java。要使用commons FileUpload套件所提供的功能來處理上傳檔案,首先要匯入下列package:
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portletcontainer.PortletRequestImpl;
另外,主要處理上傳檔案的部分就是在actionPerformed方法中,範例程式碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlet.event.*;
import utilities.HttpMultiPartParser;
 
public class FileUploadPortlet extends PortletAdapter implements ActionListener{
 
    public void login(PortletRequest request) throws PortletException {
        PortletSession session = request.getPortletSession();
        session.setAttribute("STATUS", "INITIAL");
        ....
    }
 
    public void doView(PortletRequest request, PortletResponse response)
        throws PortletException, IOException {
        super.doView(request, response);
        PortletSession session = request.getPortletSession();
        String status = (String) session.getAttribute("STATUS");
        String page = "/jsp/FileUploadPortletView/Initial.jsp";
 
        if(status.equals("UPLOADED")) {
            page = "/jsp/FileUploadPortletView/Uploaded.jsp";
        } else if(status.equals("EXCEPTION")) {
            page = "/jsp/FileUploadPortletView/Exception.jsp";
        }
        this.getPortletConfig().getContext().include(page, request, response);
    }
 
    public void actionPerformed(ActionEvent event) throws PortletException {
        PortletRequest request = event.getRequest();
        DiskFileUpload upload = new DiskFileUpload();
        [color=Red]List itemList = upload.parseRequest(((PortletRequestImpl)request).getProxiedHttpServletRequest());[/color]
        String uploadFilesFolder = "/WSAD51/workspace/FileUpload/WebContent/upload/";
        Iterator itr = itemList.iterator();
        try {
            while(itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if(item.getContentType() == null) {
                    //這是普通表單欄位
                    String formFieldName = item.getFieldName();
                    String FormFieldValue = item.getString());
                    ....
                } else {    //這是檔案表單欄位
                   /**取得完整檔案名稱,
                    *  此方法會取得此檔案在client 端的完整路徑 
                    *  Ex : "C:\\Pictures\\image001.gif"
                    */
                    String clientFileName = item.getName();
                    //將此檔案儲存在server 端,檔名自訂,此處寫法僅為示範
                    File uploadfile = new File(uploadFilesFolder + "image001.gif");
                    fileItem.write(uploadfile);
                }
            }
        } catch (IOException ioe) {
            System.err.println("IOException on FileUploadPortlet.actionPerformed");
            ioe.printStackTrace();
            session.setAttribute("STATUS", "EXCEPTION");
            session.setAttribute("EXCEPTION", ioe.getMessage());
        } catch (Exception e) {
            System.err.println("Exception on FileUploadPortlet.actionPerformed");
            e.printStackTrace();
            session.setAttribute("STATUS", "EXCEPTION");
            session.setAttribute("EXCEPTION", e.getMessage());
        }
    }
}

其中比較值得注意的地方是紅色那行,由於FileUpload套件的MultiPartRequestParser (此處為DiskFileUpload)的parse方法接受的參數(shù)為HttpServletRequest,但是我們在Portlet中得到的卻是PortletRequest,而這兩種request類別之間似乎無直接繼承關(guān)係,因此使用特別方式將PortletRequest轉(zhuǎn)換成HttpServletRequest (見範例程式第5行),再交給MultiPartRequestParser處理。由此可看出,似乎PortletRequest中包含了HttpServletRequest。另外這種偷吃步的用法目前僅知適用於Portal 4.2 API,至於Portal 5.x API則尚未確定,如果有其他人有適用於Portal 5.x API 的檔案上傳方法,也希望能提供分享Big Smile。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多