隨筆-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作為框架的。以下是例子所依賴(lài)類(lèi)包的列表:

          依賴(lài)類(lèi)包的列表?
          清單1 依賴(lài)類(lèi)包的列表

          首先,創(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類(lèi)型)和myFileFileName(上傳文件的文件名,該文件名不包括文件的路徑)。因此,<s:file name="xxx" />對(duì)應(yīng)Action類(lèi)里面的xxx、xxxContentType和xxxFileName三個(gè)屬性。

          FileUploadAction作用是將瀏覽器上傳的文件拷貝到WEB應(yīng)用程序的UploadImages文件夾下,新文件的名稱(chēng)是由系統(tǒng)時(shí)間與上傳文件的后綴組成,該名稱(chēng)將被賦給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)的話(huà),應(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)該阻止用戶(hù)上傳非圖片類(lèi)型的文件。在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)到名稱(chēng)為“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)類(lèi)似:

          ? ? 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)類(lèi)似:

          ? ? 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 閱讀(108651) 評(píng)論(148)  編輯  收藏 所屬分類(lèi): Struts 2.0系列
          評(píng)論共2頁(yè): 1 2 下一頁(yè) 

          評(píng)論:
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-08-29 13:50 | Ouch
          好像還應(yīng)該加入包servlet-api.jar !!!  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-03 16:58 | li
          好像filename取出來(lái)的是null呀,有人碰到這種情況嗎?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-09-04 09:40 | Mike
          我取出來(lái)的filename也是null啊,到底怎么搞的,有些地方?jīng)]有講清楚  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-04 16:13 | tf
          我取出的也是null,
          運(yùn)行出現(xiàn)下面的錯(cuò)誤提示,也就是值都沒(méi)有傳過(guò)去.請(qǐng)高手指教
          type Exception report

          message

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

          exception

          java.lang.NullPointerException
          tutorial.FileUpload.getExtention(FileUpload.java:76)
          tutorial.FileUpload.execute(FileUpload.java:82)
          sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          java.lang.reflect.Method.invoke(Method.java:585)
          com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:334)
          com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:221)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:195)
          com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:118)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:155)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:180)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:204)
          com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:25)
          org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:24)
          org.apache.struts2.impl.RequestContextImpl.callInContext(RequestContextImpl.java:147)
          org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:23)
          org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:317)
          org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:242)

            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-04 17:03 | tf
          怎么沒(méi)有人回答?請(qǐng)高手指教  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-05 00:35 | Max
          @Mike
          @tf
          請(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)文件上傳[未登錄](méi) 2007-09-07 15:59 | Mike
          @li
          @tf
          我想我大概知道錯(cuò)誤的原因了,你們可能是漏了一些get/set方法,或者是自己在struts.xml中加入了<interceptor name ="fileUploadStack" class ="tutorial.FileUploadInterceptor" />,這個(gè)不需要自己加入的,是struts2內(nèi)置的interceptor,會(huì)自動(dòng)調(diào)用。我希望我的解決方法可以對(duì)你們有用,這里也要對(duì)max說(shuō)聲對(duì)不起了,沒(méi)有跑出來(lái)是自己沒(méi)按照規(guī)則來(lái)做。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-10 16:59 | tf
          請(qǐng)問(wèn)Max可以出個(gè)和文件上傳對(duì)應(yīng)的文件下載的例子嗎?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-10 17:09 | tf
          誰(shuí)有和文件下載的的例子的麻煩發(fā)到我郵箱好嗎?
          我的郵箱是susu_zi@126.com  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-12 15:53 | shenchong
          Max,請(qǐng)問(wèn)一下為什么我在做多附件上傳的時(shí)候uploads里面的內(nèi)容是String型的,而不是File類(lèi)型的  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-13 00:19 | Max
          @tf
          Struts 2 的Show Case中有相關(guān)的例子
          @shenchong
          你看錯(cuò)了吧?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-13 11:04 | laibin
          學(xué)習(xí)中!
          在此感謝Max!  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-13 17:46 | shenchong
          @Max
          我沒(méi)有看錯(cuò)的,我試過(guò)了show case中的相關(guān)例子,它拿到的的確是List類(lèi)型的,但我拿到的卻是String的,是不是還需要在哪里配置?或者是jar包的版本問(wèn)題?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-14 09:53 | ILOVEYOU

          @Max
          我現(xiàn)在出現(xiàn)如下異常,圖片顯示不出來(lái)

          java.io.FileNotFoundException: D:\Java\jetty-6.1.3\webapps\uploadtest\UploadImages\1189733933484.bmp (系統(tǒng)找不到指定的路徑。)
           at java.io.FileOutputStream.open(Native Method)
           at java.io.FileOutputStream.
          <init>(FileOutputStream.java:179)
           at java.io.FileOutputStream.
          <init>(FileOutputStream.java:131)
           at tutorial.FileUploadAction.copy(FileUploadAction.java:
          87)
           at tutorial.FileUploadAction.execute(FileUploadAction.java:
          114)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
          39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
          25)
           at java.lang.reflect.Method.invoke(Method.java:
          585)
           at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:
          404)
           at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:
          267)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          229)
           at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:
          123)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
          167)
           at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
          86)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:
          83)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:
          121)
           at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
          86)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:
          170)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:
          176)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:
          268)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:224)
           at com.opensymphony.xwork2.DefaultActionInvocation$
          2.doProfiling(DefaultActionInvocation.java:223)
           at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:
          455)
           at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
          221)
           at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:
          50)
           at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
          504)
           at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:
          419)
           at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:
          1089)
           at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:
          99)
           at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:
          1089)
           at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:
          365)
           at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:
          216)
           at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:
          181)
           at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:
          712)
           at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
          405)
           at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:
          211)
           at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:
          114)
           at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
          139)
           at org.mortbay.jetty.Server.handle(Server.java:
          285)
           at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:
          502)
           at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:
          835)
           at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:
          641)
           at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:
          208)
           at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:
          378)
           at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:
          368)
           at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:
          442)
          2007-9-14 9:38:54 org.apache.struts2.interceptor.FileUploadInterceptor intercept
          信息: Removing file myFile \tmp\upload_4b5952e6_11501ab2110__8000_00000000.tmp


          大家?guī)臀铱纯?br />

            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-09-14 22:34 | leson
          提醒一下,MAX示例代碼中拷貝文件部分:
          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);
          }

          有點(diǎn)問(wèn)題,如果文件的大小,%BUFFERSIZE的余數(shù)不為0,則會(huì)造成多拷貝了N多K字節(jié),又加之buffer[]里面剩余部分沒(méi)有被覆蓋,導(dǎo)致最后的文件和原來(lái)的文件前面部分相等,后面的部分就沒(méi)有多少規(guī)律了,但卻又不是全空。

          修正方法是:
          byte[] buffer = new byte[BUFFER_SIZE];
          for (int byteRead = 0; (byteRead = in.read(buffer)) > 0; )
          {
          out.write(buffer, 0, byteRead);
          }
          最后一次寫(xiě)入該寫(xiě)的字節(jié)就可以了。
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-09-26 22:14 | Jimmy
          文件類(lèi)型里明明有jpeg,png的,為什么不能上傳?
          Content-Type not allowed: myFile "upload_3eb7e010_115421d641d__8000_00000032.tmp" image/pjpeg

          Content-Type not allowed: myFile "upload_3eb7e010_115421d641d__8000_00000034.tmp" image/x-png  回復(fù)  更多評(píng)論
            
          # 遇到了同樣的問(wèn)題 2007-10-09 12:00 | referee
          上面有人出現(xiàn)的問(wèn)題我也遇到了,搞不來(lái),哪位高人幫幫忙
          java.io.FileNotFoundException: F:\cvs_root\struts\WebContent\UploadImages\1191902350707.bmp (系統(tǒng)找不到指定的路徑。)
          at java.io.FileOutputStream.open(Native Method)
          at java.io.FileOutputStream.<init>(Unknown Source)
          at java.io.FileOutputStream.<init>(Unknown Source)
          at tutorial.FileUploadAction.copy(FileUploadAction.java:55)
          at tutorial.FileUploadAction.execute(FileUploadAction.java:84)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:334)
          at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:221)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:195)
          at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:118)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:155)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:180)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:266)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:193)
          at org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:25)
          at org.apache.struts2.impl.StrutsActionProxy$1.call(StrutsActionProxy.java:24)
          at org.apache.struts2.impl.RequestContextImpl.callInContext(RequestContextImpl.java:147)
          at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:23)
          at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:317)
          at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:242)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
          at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
          at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
          at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
          at java.lang.Thread.run(Unknown Source)
          2007-10-9 11:59:10 org.apache.struts2.interceptor.FileUploadInterceptor intercept
          信息: Removing file myFile \tmp\upload__7d878dc9_11582e8d4f6__8000_00000002.tmp  回復(fù)  更多評(píng)論
            
          # 知道答案了 2007-10-09 12:43 | referee
          是文件夾要手動(dòng)建立  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-10-10 12:42 | hehe
          不錯(cuò),什么時(shí)候來(lái)個(gè)struts2+hibernate+spring整合,期待中。。。。。。。。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-10-31 16:57 | 赫連紫軒(puras)
          Struts2.0的上傳文件大小默認(rèn)是2M
          可以通過(guò)在struts.xml里來(lái)修改這個(gè)大小
          <constant name="struts.multipart.maxSize" value="5242880" />

          但是遇到個(gè)問(wèn)題就是
          這里假如我設(shè)置的是5M
          而我的Action實(shí)上設(shè)置的為3M
          此時(shí)我上傳4M的文件的時(shí)候
          可以捕捉到異常提示我文件過(guò)大
          返回到上傳頁(yè)面
          如果此時(shí)我上傳的文件超過(guò)了5M
          則會(huì)直接告訴我文件過(guò)大
          雖然也會(huì)返回到上傳頁(yè)面
          但之前上傳頁(yè)面的數(shù)據(jù)將會(huì)丟失
          比如說(shuō)從別的頁(yè)面?zhèn)鬟f過(guò)來(lái)的一些參數(shù).

          請(qǐng)教下該如何解決?
          謝謝.  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-01 10:27 | 游客
          max你好,我一直關(guān)注著你的struts2,從開(kāi)始接觸struts2就是看的你的博客,受益匪淺,非常感謝你的介紹,現(xiàn)在網(wǎng)上關(guān)于struts的內(nèi)容太少了。你說(shuō)的批量上傳文件,他的文件名里有系統(tǒng)時(shí)間,那同一次上傳的文件,系統(tǒng)時(shí)間是一樣的嗎  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-23 11:07 | kayata
          @游客
          遇到一個(gè)問(wèn)題,struts2和spring2集成后,在文件上傳時(shí),發(fā)現(xiàn)fileload攔截器不工作,設(shè)置的文件類(lèi)型和大小都不起作用,不知為什么?,急  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-26 09:35 | @游客
          點(diǎn)擊文件上傳時(shí)提示Invalid field value for field "myFile",你的myFile字段時(shí)File型,我把他改成String型就可以,然后做相應(yīng)的修改,請(qǐng)問(wèn)這到底時(shí)為何  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-29 09:16 | bolly
          我在上傳的action中加了攔截器以后,別的action請(qǐng)求也被攔截下來(lái)了,fileUpload是全局的嗎?怎么解決?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-29 09:37 | bolly
          <action name="dofileUpload" class="fileUploadAction"
          method="dofileUpload">
          <interceptor-ref name="fileUpload">
          <param name="allowedTypes">
          application/vnd.ms-excel,text/plain
          </param>
          </interceptor-ref>
          <interceptor-ref name="defaultStack"></interceptor-ref>
          <result name="input">WEB-INF/jsp/zzz/upload.jsp</result>
          <result>WEB-INF/jsp/zzz/upload.jsp</result>
          </action>
          <action name="uploadClear" class="fileUploadAction"
          method="uploadClear">
          <result name="input">WEB-INF/jsp/zzz/upload.jsp</result>
          <result>WEB-INF/jsp/zzz/upload.jsp</result>
          </action>
          uploadClear提交不到action中去,怎么會(huì)這樣?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-29 10:15 | zoninge
          大家?guī)臀铱纯催@個(gè)錯(cuò)誤啊,包已經(jīng)添進(jìn)去了,怎么回事呢?

          HTTP Status 404 - /photo/fileUpload

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

          type Status report

          message /photo/fileUpload

          description The requested resource (/photo/fileUpload) is not available.


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

          Apache Tomcat/5.5.15  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-29 17:38 | zoninge
          終于出來(lái)了
          相當(dāng)感謝啊
          我是小菜鳥(niǎo) 多多指教!  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-12-22 09:59 | xtmtd
          上傳一個(gè)大文件時(shí),STRUTS2的攔截器,會(huì)報(bào)錯(cuò),說(shuō)文件過(guò)大。但是一次上傳幾個(gè)大文件時(shí),STRUTS2的攔截器就不報(bào)任何錯(cuò)誤了,也沒(méi)有提示,不知道為什么啊?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-12-26 10:14 | suyuan
          啊 我也出現(xiàn)了這個(gè)問(wèn)題

          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:697)
          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.23 logs.

          filter 也配了 上傳出現(xiàn)這個(gè),。。。。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-12-29 10:20 | wendy
          這么寫(xiě)會(huì)存在并發(fā)問(wèn)題吧?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2007-12-29 21:56 | wendy
          當(dāng)我上傳一個(gè)空文件時(shí)(比如說(shuō)我建一個(gè) 新建文件.txt),報(bào)空指針異常,我覺(jué)得只要文件存在不管里面有沒(méi)有內(nèi)容都不應(yīng)該報(bào)錯(cuò)的啊,為什么?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-01-04 18:03 | ElgooG
          寫(xiě)的東西不完全對(duì)啊  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-01-09 20:15 | szz
          我想問(wèn)一下,我的form中加了enctype="multipart/form-data",配了struts2的校驗(yàn)器校驗(yàn)其他的文本框就不起作用了,去了enctype="multipart/form-data",校驗(yàn)器就好用了,這是怎么回事呀?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-01-22 01:01 | 飛飛
          當(dāng)我配置web.xml后,網(wǎng)站目錄不能瀏覽,出現(xiàn)404錯(cuò)誤!
          請(qǐng)問(wèn)這是什么問(wèn)題?
          謝謝.  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-02-20 16:11 | walkerstar
          Struts2的文件上傳只能說(shuō)是簡(jiǎn)單的文件上傳。
          如果一個(gè)網(wǎng)站文件上傳很重要的話(huà),需要仔細(xì)的考慮各種可能性。
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-02-22 13:10 | look
          我無(wú)法通過(guò)攔截器控制上傳類(lèi)型,代碼在下面,有誰(shuí)能幫我看看,我試了很久fileUploadStack這個(gè)攔截器就是不起作用,什么文件都能上傳。
          struts.xml:
          <include file ="struts-default.xml" />
          <action name="fileUpload" class="struts.FileUpLoad">
          <interceptor-ref name ="fileUploadStack" >
          <param name ="allowedTypes" >
          image/bmp,image/png,image/gif,image/jpeg
          </param >
          </interceptor-ref >
          <interceptor-ref name ="defaultStack" /> <result name ="input" >/upload.jsp</result >
          <result name="success">/show.jsp</result>

          upload.jsp:
          <s:fielderror></s:fielderror>
          <s:form action="fileUpload" method="post" enctype="multipart/form-data">
          <s:file name="myFile" label="file"></s:file>
          <s:textfield name="caption" label ="Caption"></s:textfield>
          <s:submit></s:submit>
          </s:form>
          <hr>
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-03-13 21:50 | kisssa
          @babala

          要傳.jpg格式的圖片,應(yīng)該是image/pjpeg。
          我也不知道為什么,但我按照錯(cuò)誤提示,修改后就OK了,希望對(duì)你有用  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-04-03 09:29 | xiaohu
          各位有調(diào)通多文件上傳的嗎?多文件上傳時(shí),如何把filename,contentType和頁(yè)面的<s:file>綁定呢??  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-04-14 19:53 | 才情亂舞
          如果要上傳到數(shù)據(jù)庫(kù)怎么修改Action和struts.xml配置文件(結(jié)合hibernate和spring)  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-04-25 10:21 | re
          <param name ="allowedTypes">
          image/bmp,image/png,image/gif,image/jpeg,text/xml,application/zip
          </param>

          這里這樣寫(xiě)還是不可以傳xml格式和zip格式的  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-05-12 21:48 | Hathor
          @eddie
          我也打印不出來(lái)
          不知道如何修改阻止提交文件的非圖片類(lèi)型以及
          顯示我友好的錯(cuò)誤信息  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-05-27 20:27 | cathy
          請(qǐng)問(wèn)如何更改上傳的路徑啊
          我想要更改成指定路徑啊
          請(qǐng)多多指教啊  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-05-29 06:06 | 123
          321564165  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-06-02 15:23 | loda
          asdasdsadad  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-05 11:46 | 愛(ài)愛(ài)愛(ài)
          啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-08 19:13 | 按時(shí)的
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-24 09:13 | 王仁
          還可以  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-24 10:13 | 王仁
          asdfsdfsdf  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-24 10:14 | 王仁
          愛(ài)不性  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-07-31 15:59 | xiexie
          有完整例子嗎包括LIB。發(fā)一份不勝感激cheerzan@yahoo.com.cn  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2008-08-07 13:08 | matrix
          @Max
          我作如下設(shè)置:
          struts.xml:
          <param name="maximumSize">102400</param>

          globalMessage_zh_CN.properties:
          struts.messages.error.file.too.large=上傳文件太大!

          strusts.properties:
          struts.multipart.maxSize=1048576

          當(dāng)上傳的圖片大小Size:102400<Size<1048576時(shí),會(huì)提示“上傳文件太大!”,而當(dāng)Size>1048576時(shí),頁(yè)面并不提示,盡管后臺(tái)打印出了文件太大的信息。我怎么讓它在頁(yè)面也表現(xiàn)出來(lái)呢???  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-08-18 11:38 | mzy
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2007-11-26 09:35 | @游客
          點(diǎn)擊文件上傳時(shí)提示Invalid field value for field "myFile",我的也出現(xiàn)了這個(gè)問(wèn)題,但是我解決了,因?yàn)槲沂褂玫氖?普通的FORM ,沒(méi)有使用<s:form>標(biāo)簽,但我的FORM 中,沒(méi)有添加 method='post' 屬性,添加后,可以上傳文件了。 希望對(duì)其它人有幫助。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-11-03 21:45 | yihaijian
          為什么我什么都是對(duì)的,就是圖片不顯示啊,郁悶,大家?guī)蛶兔Π?..  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-11-12 11:18 | 車(chē)水馬龍
          @yihaijian
          文件路徑錯(cuò)誤了吧  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-11-19 10:46 | lrl
          感覺(jué)跟你們相見(jiàn)恨晚
          向你們看齊·  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-11-19 10:56 | lrl
          學(xué)習(xí)struts中,每次程序錯(cuò)誤,上網(wǎng)都可以找到高人的解答方法,有種相見(jiàn)恨晚感覺(jué),努力向你們看齊  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2008-12-16 10:28 | icewind
          請(qǐng)問(wèn),為什么我上傳的struts.xml中設(shè)置的所有類(lèi)型的圖片類(lèi)型文件,都說(shuō)是
          The file you uploaded is not a image啊,還有知道的人 ?????  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-01-09 18:08 | 瑞瑞
          想不到吧這位同學(xué),2009年的1月9號(hào)我也碰到了和你同樣的問(wèn)題,弄不出來(lái),我是按照struts2中文幫助文檔做的,什么都沒(méi)錯(cuò),就是不顯示圖片,郁悶。。。。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-01-11 13:52 | reaijava
          正在學(xué)習(xí)Struts 2 支持  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-01-19 19:07 | shuzigui
          我運(yùn)行你的第二個(gè)例子的時(shí)候,就是限制文件類(lèi)型時(shí),出錯(cuò),我按照你的配置修改struts.xml,在上傳非圖片格式的文件時(shí),仍然可以上傳上去,這是怎么回事呀?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-01-19 19:18 | shuzigui
          @Joe
          你的錯(cuò)誤是在FileUploadAction.java中g(shù)etExtention方法下的fileName.lastIndexOf( "." );引號(hào)中只有一個(gè)點(diǎn),而不是有有空格加點(diǎn)的(" . ")這種,我今天碰到這種問(wèn)題,我把它改過(guò)來(lái)就可以運(yùn)行第一個(gè)例子了  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-02-05 16:39 | sheepzhao
          @zoninge
          你的問(wèn)題怎么解決的阿?我也出現(xiàn)這個(gè)問(wèn)題了  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-03-17 21:15 | 2少
          File imageFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);

          請(qǐng)問(wèn)一下,這句代碼是用來(lái)干什么的呢?


          我在編譯Action的時(shí)候,老是說(shuō) 無(wú)法訪(fǎng)問(wèn)javax.servlet.ServletContext
          然后說(shuō)上面那一句代碼有錯(cuò)誤  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-04-27 18:10 | tufu
          剛學(xué)struts2,搞此上傳文件例子時(shí)有如下錯(cuò)誤:
          java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
          懇求高人指點(diǎn)!mrzhangtufu@126.com
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-05-29 16:17 | coobee
          @ILOVEYOU

          I encountered the same problem, it's because that the directory "UpdateImages" doesn't exist. Please invoke

          FileUtils.copyFile(myFile, imageFile);

          to replace the original copy method.

          or

          Add the below code in copy method to create the parent directory when it does't exist.

          if (dst.getParentFile() != null && dst.getParentFile().exists() == false) {
          if (dst.getParentFile().mkdirs() == false) {
          throw new IOException("Destination '" + dst + "' directory cannot be created");
          }
          }
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-08-05 14:03 | 第三方
          你能說(shuō)一下怎么存到數(shù)據(jù)庫(kù)么,發(fā)到736732716@qq.com 謝謝  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-08-09 12:53 | HAHA
          太感謝 了。   回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-08-09 12:53 | HAHA
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-10-12 22:18 | 哥們
          在struts2上傳時(shí),我碰到個(gè)問(wèn)題,想請(qǐng)教
          如果某個(gè)文章記錄已經(jīng)上傳了圖片,而后來(lái)發(fā)現(xiàn)這個(gè)記錄要修改,但不需要重新上傳圖片,只需要修改其它的字段,提交后,發(fā)現(xiàn)原來(lái)上傳的圖片記錄就沒(méi)有了

          為此我還得在頁(yè)面另外隱藏一個(gè)字段,并且在action里做大段處理代碼。想請(qǐng)問(wèn)一下你是否碰到過(guò)此問(wèn)題
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2009-11-04 22:38 | zx
          存數(shù)據(jù)庫(kù)里只需要把fileName圖片名存進(jìn)去i就OK,其實(shí)也沒(méi)必要存數(shù)據(jù)庫(kù)的圖片的話(huà),放服務(wù)器上就行  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-11-27 15:08 | zc
          如果我多文件上傳,一個(gè)文件只準(zhǔn)上傳圖片格式,另外一個(gè)沒(méi)有限制了,該怎么辦?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-12-10 14:38 | wo
          請(qǐng)問(wèn)下,我照你的全都寫(xiě)了,在上傳那個(gè)頁(yè)面不管你上傳什麼都不會(huì)跳過(guò)去,根本一點(diǎn)跳動(dòng)都沒(méi)有,不知道是什麼原因?有沒(méi)高手遇到過(guò)?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-12-10 14:44 | wo
          @@wo
          我都是一字不漏的全復(fù)過(guò)來(lái)的,就是在上傳那個(gè)頁(yè)面根本就不跳,,發(fā)呆了  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2009-12-11 15:34 | wo
          您好,我想請(qǐng)問(wèn)下您寫(xiě)的第一個(gè)我試了下報(bào)出下面異常:
          imageFileName===1260516248015.jpg
          java.io.FileNotFoundException: D:\Tomcat 6.0\webapps\Struts28\UploadImages (存取被拒。)
          at java.io.FileOutputStream.open(Native Method)
          at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
          at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
          at tutorial.FileUploadAction.copy(FileUploadAction.java:57)
          at tutorial.FileUploadAction.execute(FileUploadAction.java:90)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
          at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
          com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
          at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
          at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
          at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
          at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
          at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
          at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
          我在路徑下找不到圖片,感覺(jué)好像只是得到了圖片名,圖片根本就沒(méi)有上傳進(jìn)去。。。求幫下忙  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳異常 2010-03-08 16:58 | 苗海峰
          2010-3-8 16:48:41 org.apache.struts2.dispatcher.Dispatcher getSaveDir
          信息: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir

          希望大家?guī)兔σ幌略趺刺幚?nbsp; 回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-03-17 16:50 | zm
          public void setMyFileContentType(String contentType) {
          this .contentType = contentType;
          }

          public void setMyFileFileName(String fileName) {
          this .fileName = fileName;
          }
          這兩個(gè)方法名是固定的嗎?為什么?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-03-17 16:58 | zm

          在此先感謝作者的這篇文章。
          上個(gè)問(wèn)題明白了  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-07-17 16:27 | LY
          你能跳轉(zhuǎn)ACTION嗎  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-08-22 23:05 | 鳳嵐舞
          @carlos175
          這個(gè)問(wèn)題怎么解決啊,  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-11-08 15:47 | 廣州用戶(hù)
          @carlos175
          可以用freemarker,struts2默認(rèn)有擴(kuò)展了freemarker,它生成的靜態(tài)頁(yè)也容易給百度,google找到。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2010-11-19 10:15 | yfjtd
          try {
          in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
          out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
          byte[] buffer = new byte[BUFFER_SIZE];
          for (int byteRead = 0; (byteRead = in.read(buffer)) > 0; )
          {
          out.write(buffer, 0, byteRead);
          }
          } finally
          {
          if (null != in)
          {
          in.close();
          }
          if (null != out)
          {
          out.close();
          }
          }
          } catch (Exception e)
          {
          e.printStackTrace();
          }

          BUFFER_SIZE這個(gè)地方報(bào)錯(cuò),不知道咋回事啊?  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-01-08 11:58 | yupan6
          遇到的問(wèn)題應(yīng)該是action中 一些字段里存在的空格引起的問(wèn)題  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-08-17 09:30 | 櫻花草
          在WebRoot下建立一個(gè)UploadImages文件夾試試  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-08-17 09:37 | 櫻花草
          我上傳的文件在tomcat中有 但是不顯示到我在項(xiàng)目中建立的文件夾中怎么回事  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-08-23 14:23 | Blur
          @櫻花草
          這個(gè)問(wèn)題很簡(jiǎn)單,你的項(xiàng)目是放在myeclipse的工作空間的,圖片上傳的路徑是服務(wù)器的路徑,所以項(xiàng)目上肯定是沒(méi)有的,只有tomcat上有圖片  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-08-31 14:09 | 嚴(yán)學(xué)智
          java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
          at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:136)
          at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:476)
          at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:486)
          at com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:517)
          at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:542)
          at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:515)
          at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:697)
          at org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:334)
          at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:394)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
          at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
          at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
          at java.lang.Thread.run(Thread.java:595)
          Caused by: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
          at org.apache.struts2.config.BeanSelectionProvider$ObjectFactoryDelegateFactory.create(BeanSelectionProvider.java:247)
          at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:134)
          ... 20 more
          求助。。。。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2011-10-11 23:54 | spring
          缺少common-fileupload.jar@renminyan
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2011-10-11 23:58 | spring
          不要直接把代碼復(fù)制過(guò)去,仔細(xì)檢查一下,會(huì)發(fā)現(xiàn)有問(wèn)題的@li
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-10-25 08:57 | zgb
          你好 我是新人 ,希望多多指點(diǎn),文件上傳到什么地方去了,如果我要把文件上傳到數(shù)據(jù)庫(kù)去 怎么辦????  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-10-25 09:09 | zgb
          @tf
          你看下setMyFileFileName有沒(méi)弄錯(cuò)  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-10-25 09:26 | zgb
          你好 我是新人 ,希望多多指點(diǎn),文件上傳到什么地方去了,如果我要把文件上傳到數(shù)據(jù)庫(kù)去 怎么辦???? 我的郵箱是zgb6219@163.com 謝謝了。  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-10-31 13:55 | zgb
          @Allen
          你好 說(shuō)說(shuō)maximumSize在哪設(shè)置30M的大小 啊 謝謝 我一直沒(méi)弄出來(lái)文件大小的設(shè)置  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2011-11-23 14:25 | lkang08
          博主您好,我照您的方法敲了代碼,不過(guò)有個(gè)問(wèn)題,就是上傳的文件不是存在web根目錄下的upload文件夾下的,而是存在
          F:\java\2011\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\zxdy\upload
          這里。請(qǐng)問(wèn)一下這是什么原因呢?該怎么解決?
          我的FileOutputStream是這樣定義的
          FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/upload")+"/"+docFileName);  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2012-04-09 09:04 | Q
          @2少
          包有沖突  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2012-05-03 16:55 | anshuqing125@sina.com
          寫(xiě)的不錯(cuò)。唯一缺點(diǎn)是粘貼后要把所有空格去掉就ok了  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2012-05-21 13:02 | kelvin
          寫(xiě)的非常好,正好解決的我的問(wèn)題,我也是剛開(kāi)始學(xué)struts2,上傳一直都報(bào)空指針,但是刷新以下后又正常了。后來(lái)自己和你的代碼核對(duì),發(fā)現(xiàn)在setter和getter還有web.xml配置上有出入,然后按照你的修改后,就不會(huì)再報(bào)空指針錯(cuò)誤了,這里特別推薦!!也非常的詳細(xì)!!  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2012-11-03 15:20 | aaa
          不知道為什么,我上傳可以成功,但其他文本框的值都取不出來(lái),不知道為什么
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳[未登錄](méi) 2013-06-07 13:49 | 果果
          @referee
          手動(dòng)文件夾建在哪個(gè)目錄下啊、  回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2014-12-31 10:09 | 李雪梅
          為什么我能跳到成功頁(yè)面但是圖片不會(huì)顯示
            回復(fù)  更多評(píng)論
            
          # re: 在Struts 2中實(shí)現(xiàn)文件上傳 2014-12-31 10:10 | 李雪梅
          @果果
          在webroot下面一個(gè)文件夾  回復(fù)  更多評(píng)論
            
          評(píng)論共2頁(yè): 1 2 下一頁(yè) 
          主站蜘蛛池模板: 济南市| 崇仁县| 榆林市| 侯马市| 红河县| 通榆县| 万盛区| 郁南县| 密山市| 嘉荫县| 巩义市| 金堂县| 来宾市| 商河县| 顺昌县| 佳木斯市| 凌海市| 浠水县| 嵊泗县| 溧水县| 龙陵县| 内乡县| 抚远县| 台山市| 辽中县| 新源县| 宁明县| 积石山| 徐州市| 上饶市| 桂阳县| 山阳县| 利川市| 万州区| 濮阳县| 江口县| 都江堰市| 探索| 荃湾区| 高雄市| 百色市|