<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-34  評(píng)論-1965  文章-0  trackbacks-0

    前一陣子有些朋友在電子郵件中問(wèn)關(guān)于Struts 2實(shí)現(xiàn)文件上傳的問(wèn)題, 所以今天我們就來(lái)討論一下這個(gè)問(wèn)題。

    實(shí)現(xiàn)原理

    Struts 2是通過(guò)Commons FileUpload文件上傳。Commons FileUpload通過(guò)將HTTP的數(shù)據(jù)保存到臨時(shí)文件夾,然后Struts使用fileUpload攔截器將文件綁定到Action的實(shí)例中。從而我們就能夠以本地文件方式的操作瀏覽器上傳的文件。

    具體實(shí)現(xiàn)

    前段時(shí)間Apache發(fā)布了Struts 2.0.6 GA,所以本文的實(shí)現(xiàn)是以該版本的Struts作為框架的。以下是例子所依賴類包的列表:

    依賴類包的列表?
    清單1 依賴類包的列表

    首先,創(chuàng)建文件上傳頁(yè)面FileUpload.jsp,內(nèi)容如下:

    <% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %>
    <% @ taglib prefix = " s " uri = " /struts-tags " %>

    <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
    < html xmlns ="http://www.w3.org/1999/xhtml" >
    < head >
    ? ?
    < title > Struts 2 File Upload </ title >
    </ head >
    < body >
    ? ?
    < s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
    ? ? ? ?
    < s:file name ="myFile" label ="Image File" />
    ? ? ? ?
    < s:textfield name ="caption" label ="Caption" /> ? ? ? ?
    ? ? ? ?
    < s:submit />
    ? ?
    </ s:form >
    </ body >
    </ html >
    清單2 FileUpload.jsp

    在FileUpload.jsp中,先將表單的提交方式設(shè)為POST,然后將enctype設(shè)為multipart/form-data,這并沒(méi)有什么特別之處。接下來(lái),<s:file/>標(biāo)志將文件上傳控件綁定到Action的myFile屬性。

    其次是FileUploadAction.java代碼:

    package tutorial;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Date;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class FileUploadAction extends ActionSupport {
    ? ?
    private static final long serialVersionUID = 572146812454l ;
    ? ?
    private static final int BUFFER_SIZE = 16 * 1024 ;
    ? ?
    ? ?
    private File myFile;
    ? ?
    private String contentType;
    ? ?
    private String fileName;
    ? ?
    private String imageFileName;
    ? ?
    private String caption;
    ? ?
    ? ?
    public void setMyFileContentType(String contentType) {
    ? ? ? ?
    this .contentType = contentType;
    ? ?}

    ? ?
    ? ?
    public void setMyFileFileName(String fileName) {
    ? ? ? ?
    this .fileName = fileName;
    ? ?}

    ? ? ? ?
    ? ?
    public void setMyFile(File myFile) {
    ? ? ? ?
    this .myFile = myFile;
    ? ?}

    ? ?
    ? ?
    public String getImageFileName() {
    ? ? ? ?
    return imageFileName;
    ? ?}

    ? ?
    ? ?
    public String getCaption() {
    ? ? ? ?
    return caption;
    ? ?}


    ? ?
    public void setCaption(String caption) {
    ? ? ? ?
    this .caption = caption;
    ? ?}

    ? ?
    ? ?
    private static void copy(File src, File dst) {
    ? ? ? ?
    try {
    ? ? ? ? ? ?InputStream in
    = null ;
    ? ? ? ? ? ?OutputStream out
    = null ;
    ? ? ? ? ? ?
    try { ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? ? ?in
    = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
    ? ? ? ? ? ? ? ?out
    = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
    ? ? ? ? ? ? ? ?
    byte [] buffer = new byte [BUFFER_SIZE];
    ? ? ? ? ? ? ? ?
    while (in.read(buffer) > 0 ) {
    ? ? ? ? ? ? ? ? ? ?out.write(buffer);
    ? ? ? ? ? ? ? ?}

    ? ? ? ? ? ?}
    finally {
    ? ? ? ? ? ? ? ?
    if ( null != in) {
    ? ? ? ? ? ? ? ? ? ?in.close();
    ? ? ? ? ? ? ? ?}

    ? ? ? ? ? ? ? ?
    if ( null != out) {
    ? ? ? ? ? ? ? ? ? ?out.close();
    ? ? ? ? ? ? ? ?}

    ? ? ? ? ? ?}

    ? ? ? ?}
    catch (Exception e) {
    ? ? ? ? ? ?e.printStackTrace();
    ? ? ? ?}

    ? ?}

    ? ?
    ? ?
    private static String getExtention(String fileName) {
    ? ? ? ?
    int pos = fileName.lastIndexOf( " . " );
    ? ? ? ?
    return fileName.substring(pos);
    ? ?}


    ? ?@Override
    ? ?
    public String execute() ? ? { ? ? ? ?
    ? ? ? ?imageFileName
    = new Date().getTime() + getExtention(fileName);
    ? ? ? ?File imageFile
    = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);
    ? ? ? ?copy(myFile, imageFile);
    ? ? ? ?
    return SUCCESS;
    ? ?}

    ? ?
    }
    清單3 tutorial/FileUploadAction.java

    在FileUploadAction中我分別寫(xiě)了setMyFileContentType、setMyFileFileName、setMyFile和setCaption四個(gè)Setter方法,后兩者很容易明白,分別對(duì)應(yīng)FileUpload.jsp中的<s:file/>和<s:textfield/>標(biāo)志。但是前兩者并沒(méi)有顯式地與任何的頁(yè)面標(biāo)志綁定,那么它們的值又是從何而來(lái)的呢?其實(shí),<s:file/>標(biāo)志不僅僅是綁定到myFile,還有myFileContentType(上傳文件的MIME類型)和myFileFileName(上傳文件的文件名,該文件名不包括文件的路徑)。因此,<s:file name="xxx" />對(duì)應(yīng)Action類里面的xxx、xxxContentType和xxxFileName三個(gè)屬性。

    FileUploadAction作用是將瀏覽器上傳的文件拷貝到WEB應(yīng)用程序的UploadImages文件夾下,新文件的名稱是由系統(tǒng)時(shí)間與上傳文件的后綴組成,該名稱將被賦給imageFileName屬性,以便上傳成功的跳轉(zhuǎn)頁(yè)面使用。

    下面我們就來(lái)看看上傳成功的頁(yè)面:

    <% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %>
    <% @ taglib prefix = " s " uri = " /struts-tags " %>

    <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
    < html xmlns ="http://www.w3.org/1999/xhtml" >
    < head >
    ? ?
    < title > Struts 2 File Upload </ title >
    </ head >
    < body >
    ? ?
    < div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
    ? ? ? ?
    < img src ='UploadImages/<s:property value ="imageFileName" /> ' />
    ? ? ? ?
    < br />
    ? ? ? ?
    < s:property value ="caption" />
    ? ?
    </ div >
    </ body >
    </ html >
    清單4 ShowUpload.jsp

    ShowUpload.jsp獲得imageFileName,將其UploadImages組成URL,從而將上傳的圖像顯示出來(lái)。

    然后是Action的配置文件:

    <? xml version="1.0" encoding="UTF-8" ?>

    <! DOCTYPE struts PUBLIC
    ? ? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    ? ? "http://struts.apache.org/dtds/struts-2.0.dtd"
    >

    < struts >
    ? ?
    < package name ="fileUploadDemo" extends ="struts-default" >
    ? ? ? ?
    < action name ="fileUpload" class ="tutorial.FileUploadAction" >
    ? ? ? ? ? ?
    < interceptor-ref name ="fileUploadStack" />
    ? ? ? ? ? ?
    < result name ="success" > /ShowUpload.jsp </ result >
    ? ? ? ?
    </ action >
    ? ?
    </ package >
    </ struts >
    清單5 struts.xml

    fileUpload Action顯式地應(yīng)用fileUploadStack的攔截器。

    最后是web.xml配置文件:

    <? xml version="1.0" encoding="UTF-8" ?>
    < web-app id ="WebApp_9" version ="2.4"
    ? ? xmlns
    ="http://java.sun.com/xml/ns/j2ee"
    ? ? xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
    ? ? xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    ? ?
    < display-name > Struts 2 Fileupload </ display-name >

    ? ?
    < filter >
    ? ? ? ?
    < filter-name > struts-cleanup </ filter-name >
    ? ? ? ?
    < filter-class >
    ? ? ? ? ? ? org.apache.struts2.dispatcher.ActionContextCleanUp
    ? ? ? ?
    </ filter-class >
    ? ?
    </ filter >
    ? ??
    ? ?
    < filter >
    ? ? ? ?
    < filter-name > struts2 </ filter-name >
    ? ? ? ?
    < filter-class >
    ? ? ? ? ? ? org.apache.struts2.dispatcher.FilterDispatcher
    ? ? ? ?
    </ filter-class >
    ? ?
    </ filter >
    ? ??
    ? ?
    < filter-mapping >
    ? ? ? ?
    < filter-name > struts-cleanup </ filter-name >
    ? ? ? ?
    < url-pattern > /* </ url-pattern >
    ? ?
    </ filter-mapping >

    ? ?
    < filter-mapping >
    ? ? ? ?
    < filter-name > struts2 </ filter-name >
    ? ? ? ?
    < url-pattern > /* </ url-pattern >
    ? ?
    </ filter-mapping >

    ? ?
    < welcome-file-list >
    ? ? ? ?
    < welcome-file > index.html </ welcome-file >
    ? ?
    </ welcome-file-list >

    </ web-app >
    清單6 WEB-INF/web.xml

    發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器地址欄中鍵入:http://localhost:8080/Struts2_Fileupload/FileUpload.jsp,出現(xiàn)圖示頁(yè)面:


    清單7 FileUpload頁(yè)面

    選擇圖片文件,填寫(xiě)Caption并按下Submit按鈕提交,出現(xiàn)圖示頁(yè)面:


    清單8 上傳成功頁(yè)面

    更多配置

    在運(yùn)行上述例子,如果您留心一點(diǎn)的話,應(yīng)該會(huì)發(fā)現(xiàn)服務(wù)器控制臺(tái)有如下輸出:

    Mar 20 , 2007 4 : 08 : 43 PM org.apache.struts2.dispatcher.Dispatcher getSaveDir
    INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
    Mar
    20 , 2007 4 : 08 : 43 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
    INFO: Removing file myFile C:\Program Files\Tomcat
    5.5 \work\Catalina\localhost\Struts2_Fileupload\upload_251447c2_1116e355841__7ff7_00000006.tmp
    清單9 服務(wù)器控制臺(tái)輸出

    上述信息告訴我們,struts.multipart.saveDir沒(méi)有配置。struts.multipart.saveDir用于指定存放臨時(shí)文件的文件夾,該配置寫(xiě)在struts.properties文件中。例如,如果在struts.properties文件加入如下代碼:

    struts.multipart.saveDir = /tmp
    清單10 struts配置

    這樣上傳的文件就會(huì)臨時(shí)保存到你根目錄下的tmp文件夾中(一般為c:\tmp),如果此文件夾不存在,Struts 2會(huì)自動(dòng)創(chuàng)建一個(gè)。

    錯(cuò)誤處理

    上述例子實(shí)現(xiàn)的圖片上傳的功能,所以應(yīng)該阻止用戶上傳非圖片類型的文件。在Struts 2中如何實(shí)現(xiàn)這點(diǎn)呢?其實(shí)這也很簡(jiǎn)單,對(duì)上述例子作如下修改即可。

    首先修改FileUpload.jsp,在<body>與<s:form>之間加入“<s:fielderror />”,用于在頁(yè)面上輸出錯(cuò)誤信息。

    然后修改struts.xml文件,將Action fileUpload的定義改為如下所示:

    ? ? ? ? < action name ="fileUpload" class ="tutorial.FileUploadAction" >
    ? ? ? ? ? ?
    < interceptor-ref name ="fileUpload" >
    ? ? ? ? ? ? ? ?
    < param name ="allowedTypes" >
    ? ? ? ? ? ? ? ? ? ? image/bmp,image/png,image/gif,image/jpeg
    ? ? ? ? ? ? ? ?
    </ param >
    ? ? ? ? ? ?
    </ interceptor-ref >
    ? ? ? ? ? ?
    < interceptor-ref name ="defaultStack" /> ? ? ? ? ? ?
    ? ? ? ? ? ?
    < result name ="input" > /FileUpload.jsp </ result >
    ? ? ? ? ? ?
    < result name ="success" > /ShowUpload.jsp </ result >
    ? ? ? ?
    </ action >
    清單11 修改后的配置文件

    顯而易見(jiàn),起作用就是fileUpload攔截器的allowTypes參數(shù)。另外,配置還引入defaultStack它會(huì)幫我們添加驗(yàn)證等功能,所以在出錯(cuò)之后會(huì)跳轉(zhuǎn)到名稱為“input”的結(jié)果,也即是FileUpload.jsp。

    發(fā)布運(yùn)行應(yīng)用程序,出錯(cuò)時(shí),頁(yè)面如下圖所示:


    清單12 出錯(cuò)提示頁(yè)面

    上面的出錯(cuò)提示是Struts 2默認(rèn)的,大多數(shù)情況下,我們都需要自定義和國(guó)際化這些信息。通過(guò)在全局的國(guó)際資源文件中加入“struts.messages.error.content.type.not.allowed=The file you uploaded is not a image”,可以實(shí)現(xiàn)以上提及的需求。對(duì)此有疑問(wèn)的朋友可以參考我之前的文章《在Struts 2.0中國(guó)際化(i18n)您的應(yīng)用程序》。

    實(shí)現(xiàn)之后的出錯(cuò)頁(yè)面如下圖所示:


    清單13 自定義出錯(cuò)提示頁(yè)面

    同樣的做法,你可以使用參數(shù)“maximumSize”來(lái)限制上傳文件的大小,它對(duì)應(yīng)的字符資源名為:“struts.messages.error.file.too.large”。

    字符資源“struts.messages.error.uploading”用提示一般的上傳出錯(cuò)信息。

    多文件上傳

    與單文件上傳相似,Struts 2實(shí)現(xiàn)多文件上傳也很簡(jiǎn)單。你可以將多個(gè)<s:file />綁定Action的數(shù)組或列表。如下例所示。

    < s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" >
    ? ?
    < s:file label ="File (1)" name ="upload" />
    ? ?
    < s:file label ="File (2)" name ="upload" />
    ? ?
    < s:file label ="FIle (3)" name ="upload" />
    ? ?
    < s:submit />
    </ s:form >
    清單14 多文件上傳JSP代碼片段

    如果你希望綁定到數(shù)組,Action的代碼應(yīng)類似:

    ? ? private File[] uploads;
    ? ?
    private String[] uploadFileNames;
    ? ?
    private String[] uploadContentTypes;

    ? ?
    public File[] getUpload() { return this .uploads; }
    ? ?
    public void setUpload(File[] upload) { this .uploads = upload; }

    ? ?
    public String[] getUploadFileName() { return this .uploadFileNames; }
    ? ?
    public void setUploadFileName(String[] uploadFileName) { this .uploadFileNames = uploadFileName; }

    ? ?
    public String[] getUploadContentType() { return this .uploadContentTypes; }
    ? ?
    public void setUploadContentType(String[] uploadContentType) { this .uploadContentTypes = uploadContentType; }
    清單15 多文件上傳數(shù)組綁定Action代碼片段

    如果你想綁定到列表,則應(yīng)類似:

    ? ? private List < File > uploads = new ArrayList < File > ();
    ? ?
    private List < String > uploadFileNames = new ArrayList < String > ();
    ? ?
    private List < String > uploadContentTypes = new ArrayList < String > ();

    ? ?
    public List < File > getUpload() {
    ? ? ? ?
    return this .uploads;
    ? ?}

    ? ?
    public void setUpload(List < File > uploads) {
    ? ? ? ?
    this .uploads = uploads;
    ? ?}


    ? ?
    public List < String > getUploadFileName() {
    ? ? ? ?
    return this .uploadFileNames;
    ? ?}

    ? ?
    public void setUploadFileName(List < String > uploadFileNames) {
    ? ? ? ?
    this .uploadFileNames = uploadFileNames;
    ? ?}


    ? ?
    public List < String > getUploadContentType() {
    ? ? ? ?
    return this .uploadContentTypes;
    ? ?}

    ? ?
    public void setUploadContentType(List < String > contentTypes) {
    ? ? ? ?
    this .uploadContentTypes = contentTypes;
    ? ?}
    清單16 多文件上傳列表綁定Action代碼片段

    總結(jié)

    在Struts 2中實(shí)現(xiàn)文件上傳的確是輕而易舉,您要做的只是使用<s:file />與Action的屬性綁定。這又一次有力地證明了Struts 2的簡(jiǎn)單易用。

    posted on 2007-03-21 00:48 Max 閱讀(108648) 評(píng)論(148)  編輯  收藏 所屬分類: Struts 2.0系列
    評(píng)論共2頁(yè): 上一頁(yè) 1 2 

    評(píng)論:
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-21 16:33 | 大菜蟲(chóng)
    max大大我現(xiàn)在剛開(kāi)始學(xué)習(xí)Struts 2.0.6按照您在Struts 2.0系列之一里的方法去做可是生成war文件上傳到tomcat上,tomcat啟動(dòng)都報(bào)錯(cuò),我用的是zip的tomcat5.5.23。我發(fā)現(xiàn)在Struts 2.0.6中沒(méi)有struts2-api.jar這個(gè)文件。是tomcat配置出了問(wèn)題。還是沒(méi)有struts2-api.jar這個(gè)文件的原因。  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-21 16:39 | 山風(fēng)小子
    您寫(xiě)的Struts2系列很詳盡,在此表示感謝!  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-22 09:27 | 太陽(yáng)里的雪
    不錯(cuò)的資料,Struts2本來(lái)就是Webwork的后續(xù)版本,學(xué)了Webwork就等于學(xué)習(xí)了Struts2.  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-23 09:15 | yangdamao
    請(qǐng)問(wèn)如何查看服務(wù)器控制臺(tái)輸出?----多多指教  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-23 09:54 | Max
    @yangdamao
    不同服務(wù)器,有不同的方法,建議通過(guò)IDE啟動(dòng)服務(wù)器,這樣可以在IDE的控制臺(tái)查看輸出。
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-23 11:39 | yangdamao
    我用的是tomcat6.0,麻煩詳細(xì)描述一下,這方面的知識(shí)嚴(yán)重欠缺,tks!  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-03-23 17:16 | evan
    呵呵!能不能寫(xiě)一個(gè)struts2 ajax方面的???
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-27 08:16 | jintian
    javax.servlet.ServletException: String index out of range: -1
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:518)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
    org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

    這是啥錯(cuò)誤??
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-28 08:13 | jintian
    在struts.xml為什么不要
    引入< include file ="struts-default.xml" /> ﹗
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-03-28 09:35 | A++
    @jintian
    我昨天也碰到了這個(gè)問(wèn)題
    你可以試著把public String execute()中的空格去掉
    方法是查找替換~~-:)  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-04-03 14:22 | Michael
    我在上傳文件的時(shí)候提示我
    com.opensymphony.xwork2.config.ConfigurationException: Unable to load bean
    org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta)
    只要把<s:form>里的 enctype ="multipart/form-data"去掉就不出這個(gè)提示了。請(qǐng)問(wèn)這是怎么回是呢?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-05 11:54 | ccz
    估計(jì)是你少加了類包!特別是那個(gè)IO的  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-09 22:30 | eddie
    請(qǐng)問(wèn)在哪里加入struts.messages.error.content.type.not.allowed=The file you uploaded is not a image,我試過(guò)很多地方都不行  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-09 22:36 | eddie
    struts.messages.error.content.type.not.allowed=The file you uploaded is not a image 后來(lái)在全局文件global_message.properties里面添加成功了,但是為什么在package.properties里面不行呢  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-09 23:26 | Max
    @eddie
    這是因?yàn)镚etText()方法是在org.apache.struts2.interceptor.FileUploadInterceptor類中被調(diào)用,而不是在你的Action類的驗(yàn)證方法中被調(diào)用。
    代碼如下:
    private String getTextMessage(String messageKey, Object[] args, Locale locale) {
    if (args == null || args.length == 0) {
    return LocalizedTextUtil.findText(this.getClass(), messageKey, locale);
    } else {
    return LocalizedTextUtil.findText(this.getClass(), messageKey, locale, DEFAULT_MESSAGE, args);
    }
    }  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-10 10:08 | furong
    我想問(wèn)一下,下載包中的lib文件夾中沒(méi)有commons-fileupload-1.1.1.jar
    commons-io-1.1.jar這兩個(gè)包,那這兩個(gè)包是不是要專門(mén)下啊  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-10 23:08 | Max
    @furong
    是的,到APACHE下載!  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-25 16:26 | ddd
    @大菜蟲(chóng)
    跟struts-api.jar沒(méi)有關(guān)系, 2.06版的沒(méi)有api包了,2.05有。。

    估計(jì)是你其他地方配置有問(wèn)題,你在本地Server上運(yùn)行沒(méi)有問(wèn)題嗎?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-04-25 16:27 | ddd
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-04-26 16:33 | Z
    請(qǐng)問(wèn)如何在struts.properties文件里用struts.multipart.maxSize對(duì)不同的上載限制不同的大小???  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-02 14:04 | 杰克
    在對(duì)多文件上傳進(jìn)行格式驗(yàn)證時(shí),由于文件同名,在報(bào)錯(cuò)時(shí)發(fā)生一人犯錯(cuò)全家株連的問(wèn)題,這個(gè)問(wèn)題該怎么辦呢?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-10 08:48 | satanxm
    請(qǐng)問(wèn)用ajax 該怎么樣上傳文件呢,
    我把你的例子中的程序 中的 form 和 submit 該為theme 改為 ajax
    就是不能用,目標(biāo)div上出來(lái)個(gè) [HTMLobject ]  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-15 12:46 | pengzhan
    有沒(méi)有關(guān)于下載的東東???  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-19 11:43 | zy
    上傳的jsp編碼似乎一定要設(shè)置成"UTF-8",GBK的話就不行了。  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-24 11:14 | 文溫
    我的struts使用的gbk編碼
    提交時(shí)經(jīng)常出現(xiàn),再刷新能顯示正常,請(qǐng)問(wèn)怎么使用gbk編碼上傳文件
    javax.servlet.ServletException
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:518)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-24 11:43 | 文溫
    對(duì)不起了
    我沒(méi)有在web.xml文件中增加過(guò)濾器

    < filter >
    < filter-name > struts-cleanup </ filter-name >
    < filter-class >
    org.apache.struts2.dispatcher.ActionContextCleanUp
    </ filter-class >
    </ filter >
    能幫忙解釋一下這個(gè)過(guò)濾器的作用是什么嗎?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-25 10:29 | Max
    @文溫
    By adding this filter, the FilterDispatcher will know to not clean up and instead defer cleanup to this filter.  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-05-28 19:00 | gpiaofei2006
    org.apache.struts2.ServletActionContext 在哪個(gè)包里啊,我的無(wú)法import  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-06-20 17:03 | jpma
    @gpiaofei2006
    在struts2-core-2.0.6.jar中!  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-06-27 10:27 | fcnh1983@163.com
    HTTP Status 404 - /uploadfile/FileUpload

    --------------------------------------------------------------------------------

    type Status report

    message /uploadfile/FileUpload

    description The requested resource (/uploadfile/FileUpload) is not available.

    為什么我按照你出現(xiàn)上面這個(gè)錯(cuò)誤??????誰(shuí)能幫忙回答下  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-03 10:12 | carlos175
    一運(yùn)行就會(huì)出現(xiàn)這個(gè)問(wèn)題是怎么回事呢?

    HTTP ERROR: 500
    Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
    RequestURI=/webapps/upload/fileUpload

    Caused by:
    java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]

    把enctype ="multipart/form-data"去掉后就會(huì)出現(xiàn):
    HTTP ERROR: 404
    NOT_FOUND
    RequestURI=/webapps/upload/fileUpload

    但是說(shuō)enctype ="multipart/form-data" 這個(gè)是必須的~
    貌似是無(wú)法加載MultiPartRequest這個(gè)東西。應(yīng)該如何解決呢?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-03 12:03 | trying
    使用這個(gè)代碼的時(shí)候總是出現(xiàn):
    HTTP ERROR: 404
    NOT_FOUND
    RequestURI=/webapps/upload/fileUpload

    這個(gè)錯(cuò)誤。是怎么回事呢?


    P.S:LS的只要導(dǎo)入commons-io那個(gè)jar就可以解問(wèn)題。  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-04 22:49 | Max
    @carlos175
    @trying
    是否缺少某些包?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-06 12:25 | carlos175
    問(wèn)題已經(jīng)都解決了。
    多謝Max的這篇文章。感覺(jué)寫(xiě)的很不錯(cuò)。
    這周剛剛開(kāi)始使用struts2。感覺(jué)和struts1.2差別還是挺大的。剛剛開(kāi)始寫(xiě)有點(diǎn)不適應(yīng)。
    特別是昨天在寫(xiě)表單處理的時(shí)候感覺(jué)布局上就變了。
    一個(gè)標(biāo)簽就一行。如果我在標(biāo)簽中加入了theme屬性的話那么就可以解決。
    恩,希望Max能夠給出一篇介紹struts2標(biāo)簽使用比較詳細(xì)的文章。再次感謝你提供了這篇不錯(cuò)的文章。  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-07-16 11:35 | Joe
    @A++

    我也遇到該問(wèn)題,我試了你說(shuō)的方法,還是報(bào)錯(cuò)java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-27 14:39 | renminyan
    HTTP Status 500 -
    -----------------------------------
    type Exception report

    message
    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
    com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:136)
    com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:476)
    com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:486)
    com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:517)
    com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:542)
    com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:515)
    org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:700)
    org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:330)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:390)
    org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)


    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


    Apache Tomcat/5.5.20


    為什么我每個(gè)例子多要調(diào)半天才出來(lái)呢?
    技術(shù)不到家max幫幫忙?希望自己快點(diǎn)長(zhǎng)進(jìn)~~~~~~~  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-07-31 12:03 | Allen
    感覺(jué)寫(xiě)的8錯(cuò)。有點(diǎn)相見(jiàn)恨晚的感覺(jué)^_^,但是我有幾個(gè)地方8懂,請(qǐng)指教。

    上傳文件最大是多大?

    上傳的進(jìn)度究竟怎么查看?

    你只列出了Image的,那么我控制別的格式的文件上傳呢?比如說(shuō)zip和XML文件

    如果文件忒大,我是否可以在上傳之前就終止上傳,太大的話Struts2好像是直接拋一個(gè)錯(cuò)誤。

    struts2最大可以上傳多大的文件,我聽(tīng)說(shuō)好像只有30Mの  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-31 23:19 | Max
    @renminyan
    請(qǐng)細(xì)心對(duì)照我文中的步驟去做,結(jié)果應(yīng)該會(huì)出來(lái)的。
    或者你的WEB-INF/web.xml的內(nèi)容,是否有加入:
    < filter >
    < filter-name > struts2 </ filter-name >
    < filter-class >
    org.apache.struts2.dispatcher.FilterDispatcher
    </ filter-class >
    </ filter >   回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-07-31 23:32 | Max
    @Allen

    1、你可以使用參數(shù)“maximumSize”來(lái)限制上傳文件的大小,它默認(rèn)值是2mb;

    2、上傳進(jìn)度需要使用AJAX技術(shù)實(shí)現(xiàn),具體你可以Google一下Ajax File Upload;

    3、你可以查看一下zip和xml的MIME類型,如果我記錯(cuò)的啊應(yīng)該分別是application/zip和text/xml;

    4、太大的文件,不建議使用HTTP的上傳,可以使用專門(mén)的FTP或者其它協(xié)議。  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-08-02 16:17 | Allen

    @Max

    謝謝MAX的解答

    1.我已經(jīng)把他設(shè)置到30M了,但是客戶如果而已上傳大文件的話還是不能捕獲異常,就讓他拋出異常吧…………

    2.Ajax Fileupload這個(gè)我會(huì)了。但是Ajax+Struts2實(shí)現(xiàn)我就8會(huì)了……因?yàn)镾truts2包裝的太嚴(yán)實(shí)了,我都無(wú)從下手的感覺(jué)。它好像直接在setter方法前邊就把他搞定了。但是setter是在execute方法之前執(zhí)行的……所以要在execute里邊實(shí)現(xiàn)這個(gè)東西貌似不太現(xiàn)實(shí)。
    是否利用攔截器可以呢?所以就這個(gè)問(wèn)題我希望方便的話可否解答下。感謝!  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-08-09 11:44 | james
    上傳文件好像不能用modelDriven模式,感覺(jué)有點(diǎn)不太舒服  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-15 14:51 | baekham
    文件上傳fileUpload攔截器allowedTypes設(shè)置疑問(wèn)?
    <interceptor-ref name="fileUpload" >
    <param name="allowedTypes">
    image/bmp,image/png,image/gif,image/jpeg
    </param>
    </interceptor-ref>
    為什么png、jpeg類型的圖片不能上傳,提示上傳文件格式錯(cuò)誤.  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-15 22:15 | 王佳
    如果在一個(gè)信息編輯頁(yè)面
    上傳圖片只是其中一個(gè)字段
    但圖片已經(jīng)上傳
    而我只是需要修改其他信息的時(shí)候
    這個(gè)時(shí)候,修改就會(huì)不能通過(guò)。
    Content-Type not allowed: myFile "upload_4e0e6f19_113d55eebb4__8000_00000007.tmp" application/octet-stream

    請(qǐng)問(wèn):有什么好的解決辦法?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-16 13:32 | zhw
    上傳時(shí)指定文件大小后
    上傳文件超過(guò)指定大小,就不能再次打開(kāi)上傳頁(yè)面。報(bào)下面的錯(cuò)誤:
    Struts Problem Report
    Struts has detected an unhandled exception:

    Messages: No result defined for action com.superweb.web.action.ResideAction and result input

    File: file:/D:/Eclipse/eclipse/workspace/SuperWeb/WebRoot/WEB-INF/classes/struts/struts_reside.xml
    Line number: 25
    Column number: 70

    只能重新啟動(dòng)服務(wù)器
    我用的是spring2+hibernate3+struts2
    請(qǐng)問(wèn):如何解決?
    dwr+struts1.2可以實(shí)現(xiàn)上傳進(jìn)度條,請(qǐng)問(wèn)在struts2中如何實(shí)現(xiàn)的?
      回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-17 12:56 | babala
    Max,

    <param name="allowedTypes">
    image/png,image/bmp,image/gif,image/jpeg,image/jpg
    </param >中,只有bmp與gif格式可以上傳,其他三個(gè)都說(shuō)格式不正確,這是為什么?
    另外為什么不直接使用png,bmp,gif....這種格式,而要在前面加上個(gè)image?  回復(fù)  更多評(píng)論
      
    # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-27 18:15 | libinbin
    回樓上 image/jpg 改成 image/JPG 就行了  回復(fù)  更多評(píng)論
      
    評(píng)論共2頁(yè): 上一頁(yè) 1 2 
    主站蜘蛛池模板: 日韩中文字幕免费视频| 内射干少妇亚洲69XXX| 乱淫片免费影院观看| 全免费a级毛片免费**视频| 亚洲一区二区三区丝袜| 夜夜嘿视频免费看| 亚洲丁香婷婷综合久久| 日本免费一二区在线电影| 亚洲av午夜电影在线观看| 情侣视频精品免费的国产| 亚洲avav天堂av在线网毛片| 日日AV拍夜夜添久久免费| 亚洲AV永久无码天堂影院| 国产成人精品免费视频大全五级| 自拍偷自拍亚洲精品播放| 国产一级理论免费版| 黄网站色视频免费看无下截| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 叮咚影视在线观看免费完整版| 亚洲精品国偷自产在线| 久久精品免费观看| 337p日本欧洲亚洲大胆艺术| 久久久久久精品免费免费自慰| 亚洲另类古典武侠| 在线观看免费成人| 色妞www精品视频免费看| 亚洲午夜精品一级在线播放放| 中文在线日本免费永久18近| 亚洲av无码乱码国产精品fc2| 99蜜桃在线观看免费视频网站| 亚洲黄色中文字幕| 成人A级毛片免费观看AV网站| 国产精品亚洲综合一区在线观看| 亚洲国产精品一区二区第一页免| 抽搐一进一出gif免费视频| 亚洲国产香蕉碰碰人人| 日本妇人成熟免费中文字幕| 亚洲欧美成aⅴ人在线观看| 在线a亚洲v天堂网2018| 中文字幕无码免费久久| 亚洲无限乱码一二三四区|