posts - 310, comments - 6939, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Struts2上傳文件示例

          Posted on 2007-10-23 12:22 詩特林 閱讀(25094) 評論(20)  編輯  收藏 所屬分類: Struts
                                                       Struts2上傳文件示例

          源代碼:Struts2Upload.rar

          1.包如下:請自行下載


          2.Action類

          package com.sterning;

          import java.io.File;

          import javax.servlet.ServletContext;

          import org.apache.commons.io.FileUtils;
          import org.apache.struts2.util.ServletContextAware;

          import com.opensymphony.xwork2.ActionSupport;

          public class StrutsFileUpload extends ActionSupport implements
                  ServletContextAware 
          {

              
          private File upload;// 實際上傳文件

              
          private String uploadContentType; // 文件的內容類型

              
          private String uploadFileName; // 上傳文件名

              
          private String fileCaption;// 上傳文件時的備注

              
          private ServletContext context;

              
          public String execute() throws Exception {

                  
          try {
                      
                      String targetDirectory 
          = context.getRealPath("/upload");
                      String targetFileName 
          = uploadFileName;
                      File target 
          = new File(targetDirectory, targetFileName);
                      FileUtils.copyFile(upload, target);            
                      
                      setUploadFileName(target.getPath());
          //保存文件的存放路徑
                  }
           catch (Exception e) {

                      addActionError(e.getMessage());

                      
          return INPUT;
                  }


                  
          return SUCCESS;

              }


              
          public String getFileCaption() {
                  
          return fileCaption;
              }


              
          public void setFileCaption(String fileCaption) {
                  
          this.fileCaption = fileCaption;
              }


              
          public File getUpload() {
                  
          return upload;
              }


              
          public void setUpload(File upload) {
                  
          this.upload = upload;
              }


              
          public String getUploadContentType() {
                  
          return uploadContentType;
              }


              
          public void setUploadContentType(String uploadContentType) {
                  
          this.uploadContentType = uploadContentType;
              }


              
          public String getUploadFileName() {
                  
          return uploadFileName;
              }


              
          public void setUploadFileName(String uploadFileName) {
                  
          this.uploadFileName = uploadFileName;
              }


              
          public void setServletContext(ServletContext context) {
                  
          this.context = context;
              }


          }


          3.頁面

          上傳頁面:upload.jsp
          <%@ page language="java" contentType="text/html; charset=GB2312"%>   
          <%@ taglib prefix="s" uri="/struts-tags" %>   
          <html>
              
          <head>
                  
          <title>文件上傳示例</title>
                  
          <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
                      type="text/css" />

              
          </head>

              
          <body>

                  
          <s:actionerror />
                  
          <s:fielderror />
                  
          <s:form action="doUpload" method="POST" enctype="multipart/form-data">
                      
          <tr>
                          
          <td colspan="2">
                              
          <h1>
                                  文件上傳示例
                              
          </h1>
                          
          </td>
                      
          </tr>

                      
          <s:file name="upload" label="上傳的文件" />
                      
          <s:textfield name="fileCaption" label="備注" />
                      
          <s:submit value="上   傳"/>
                  
          </s:form>
              
          </body>
          </html>

          上傳成功頁面:upload_success.jsp
          <%@ page language="java" contentType="text/html; charset=GB2312"%>  
          <%@ taglib prefix="s" uri="/struts-tags"%>
          <html>
              
          <head>
                  
          <title>上傳成功</title>
                  
          <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
                      type="text/css" />
              
          </head>

              
          <body>
                  
          <table class="wwFormTable">
                      
          <tr>

                          
          <td colspan="2">
                              
          <h1>
                                  上傳成功
                              
          </h1>
                          
          </td>
                      
          </tr>

                      
          <tr>
                          
          <td class="tdLabel">
                              
          <label for="doUpload_upload" class="label">
                                  內容類型:
                              
          </label>
                          
          </td>
                          
          <td>
                              
          <s:property value="uploadContentType" />
                          
          </td>
                      
          </tr>

                      
          <tr>
                          
          <td class="tdLabel">
                              
          <label for="doUpload_upload" class="label">
                                  文件路徑:
                              
          </label>
                          
          </td>
                          
          <td>
                              
          <s:property value="uploadFileName" />
                          
          </td>
                      
          </tr>


                      
          <tr>
                          
          <td class="tdLabel">
                              
          <label for="doUpload_upload" class="label">
                                  臨時文件:
                              
          </label>
                          
          </td>
                          
          <td>
                              
          <s:property value="upload" />
                          
          </td>
                      
          </tr>

                      
          <tr>
                          
          <td class="tdLabel">
                              
          <label for="doUpload_upload" class="label">
                                  備注:
                              
          </label>
                          
          </td>
                          
          <td>
                              
          <s:property value="fileCaption" />
                          
          </td>
                      
          </tr>


                  
          </table>

              
          </body>
          </html>

          4.struts.xml
          <?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>
              
          <constant name="struts.devMode" value="true" />
              
          <constant name="struts.i18n.encoding" value="GB2312" />
           
              
          <package name="NG" namespace="/" extends="struts-default">
                  
          <action name="showUpload">
                      
          <result>/upload.jsp</result>
                  
          </action>
                  
                  
          <action name="doUpload" class="com.sterning.StrutsFileUpload">
                      
          <result name="input">/upload.jsp</result>
                      
          <result>/upload_success.jsp</result>
                  
          </action>
              
          </package>

          </struts>


          5.web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app id="WebApp_ID" 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>customization</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>

          </web-app>




          評論

          # re: Struts2上傳文件示例  回復  更多評論   

          2007-10-24 12:03 by 快譯站
          總結的不錯
          java集中營http://java.co.cc誠意邀請您做開源技術struts2.0

          同時歡迎您加入java技術QQ群23133419

          # re: Struts2上傳文件示例  回復  更多評論   

          2007-10-24 12:05 by 快譯站
          java集中營地址 http://java8.co.cc

          # re: Struts2上傳文件示例[未登錄]  回復  更多評論   

          2007-10-26 09:59 by apple0668
          下載源碼加進相關的包,一提交就發射404錯誤!不知道是什么原因!連包跟源碼發我一份,謝謝!email:chensp1230@163.com

          # re: Struts2上傳文件示例  回復  更多評論   

          2007-11-14 11:19 by study
          下載源碼加進相關的包(除commons-fileupload-1.1.1 ,commons-io-1.1的版本有點區別之外,其它完全相同)一提交就產生404錯誤!不知什么原因!能否連包跟源碼發我一份,謝謝! email:zggzspring@163.com

          # re: Struts2上傳文件示例  回復  更多評論   

          2007-11-25 16:56 by shaomin
          不錯
          謝謝分享
          下來試試

          # re: Struts2上傳文件示例  回復  更多評論   

          2008-01-04 21:43 by 凌晨風
          樓主寫的不錯,這個例子好像是Struts包里的源碼,但是我不知道上傳成功后怎樣看這個文件,比如我傳了一張圖片,我怎樣才能看到我傳的圖片呢,上傳的是一個tmp的虛擬文件,我也弄不清楚,幫忙啊!

          # re: Struts2上傳文件示例[未登錄]  回復  更多評論   

          2008-01-17 09:17 by yang
          請問如何控制上傳文件的類型和大小! 謝謝!!!

          # re: Struts2上傳文件示例[未登錄]  回復  更多評論   

          2008-01-17 17:04 by yang
          請問如何控制上傳文件的類型和大小! 謝謝!!!

          # re: Struts2上傳文件示例  回復  更多評論   

          2008-03-21 10:08 by 花花
          請問要上傳視頻文件用這個能行嗎
          視頻至少都有5、6M的 這個好像只能傳2M以內的
          怎么能把限制設大點兒 或者去掉限制?

          # re:   回復  更多評論   

          2009-03-18 11:35 by fjj
          樓主,能把Struts2上傳文件示例的所需jar發給我不?謝謝!
          fanjj_023@126.com

          # re: Struts2上傳文件示例  回復  更多評論   

          2009-03-19 16:41 by fanjj_023
          樓主,為什么我測試時tomcat會報/WEB-INF/web.xml not found,還有jsp也報異常,提示下面這句
          <link href=" <s:url value="/css/examplecss"/>"rel="stylesheet" type="text/css"/> 為什么呀?

          # re: Struts2上傳文件示例  回復  更多評論   

          2009-05-07 10:50 by 打哈十分客戶
          一個個只會叫,都不會自己想辦法啊!!!lz講的很透徹!!!

          # re: Struts2上傳文件示例[未登錄]  回復  更多評論   

          2009-05-19 11:19 by aaa
          看到 上面那些 有錯誤的.感覺好笑,

          這么差的自學能力,還學什么 Java ?

          LZ 的這個例子,應用太簡單了,文件類型,大小,多文件的上傳

          以及 Action 類里面引用了 Servlet ,這些都還沒處理呀...

          # re: Struts2上傳文件示例  回復  更多評論   

          2009-05-27 12:05 by ss
          水平太低不懂就問,有錯嗎?難道你們一生下來就會..從沒問過一些簡單的問題?

          # re: Struts2上傳文件示例  回復  更多評論   

          2009-06-10 22:02 by kinble
          不知道為什么總是報錯:
          java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
          通過網上查找,都說是缺少了commons-io包,但我很肯定我是已經導進去的了!

          # re: Struts2上傳文件示例  回復  更多評論   

          2010-02-24 11:20 by 一根煙
          借鑒了你的代碼 謝謝

          # re: Struts2上傳文件示例  回復  更多評論   

          2010-05-31 15:31 by 張三
          可以啊

          # re: Struts2上傳文件示例  回復  更多評論   

          2011-05-19 22:54 by ....
          不就是把struts2教程里的東西拷進來了嘛,又不是你自己寫的

          # re: Struts2上傳文件示例[未登錄]  回復  更多評論   

          2012-02-14 15:39 by kevin
          測試好用 攢人品感謝樓主~~

          # re: Struts2上傳文件示例  回復  更多評論   

          2012-03-16 08:15 by zhen
          Java程序員之家 QQ:群號2218986 歡迎大家加入
          主站蜘蛛池模板: 威宁| 靖江市| 开江县| 千阳县| 商南县| 赤峰市| 济源市| 龙川县| 五河县| 隆安县| 青神县| 政和县| 哈尔滨市| 潜江市| 中方县| 永年县| 清涧县| 六安市| 特克斯县| 湛江市| 棋牌| 津市市| 清涧县| 集安市| 自贡市| 永登县| 正定县| 庆云县| 灵川县| 兰坪| 瓦房店市| 边坝县| 开远市| 敦煌市| 蒙山县| 中山市| 高青县| 甘南县| 富裕县| 宁蒗| 昌都县|