struts2的文件上傳

          一直以來自己都是看書的時候多,而實際做卻很少,
          最近慢慢開始做東西了,發現,看與做是2個完全不同的事情。
          記錄下面的東西是為了以后使用方便。
          利用struts2上傳文件。
          上傳頁面
                 <form action="upload" method="post" enctype="multipart/form-data">
                     文件標題:
          <input type="text" name="title" /><br>
                     選擇文件:
          <input type="file" name="upload" /><br>
                     
          <input value="上傳" type="submit" />
                 
          </form>
          然后就是struts.xml文件,在  <package>標簽中
                  <action name="upload" class="com.duduli.li.Upload">
                      
          <param name="savePath">/file</param>
                      
          <result name="showUpload">showUpload.jsp</result>
                  
          </action>
          然后就是處理上傳的文件。
          package com.duduli.li;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.ActionSupport;

          @SuppressWarnings(
          "serial")
          public class Upload extends ActionSupport {
              
          private String title;
              
          private File upload;
              
          private String uploadContextType;
              
          private String uploadFileName;
              
          private String savePath;

              
          public String getTitle() {
                  
          return title;
              }

              
          public void setTitle(String title) {
                  
          this.title = title;
              }

              
          public File getUpload() {
                  
          return upload;
              }

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

              
          public String getUploadContextType() {
                  
          return uploadContextType;
              }

              
          public void setUploadContextType(String uploadContextType) {
                  
          this.uploadContextType = uploadContextType;
              }

              
          public String getUploadFileName() {
                  
          return uploadFileName;
              }

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

          //通過struts2的配置文件得到上傳目錄,這個是很重要的
              @SuppressWarnings("deprecation")
              
          public String getSavePath() {
                  
          return ServletActionContext.getRequest().getRealPath(savePath);
              }

              
          public void setSavePath(String value) {
                  
          this.savePath = value;
              }

              @Override
              
          public String execute() throws Exception {
                  FileOutputStream fos 
          = new FileOutputStream(getSavePath() + "\\"
                          
          + getUploadFileName());
                  FileInputStream fis 
          = new FileInputStream(getUpload());
                  
          byte[] buffer = new byte[1024];
                  
          int len = 0;
                  
          while ((len = fis.read(buffer)) > 0) {
                      fos.write(buffer, 
          0, len);
                  }
                  
          return "showUpload";
              }
          }

          如果你認為配置這些就可以了,那你就錯了,還需要配置一個struts.properties文件。定義一個臨時的上傳文件夾。
          #配置臨時存放目錄
          struts.multipart.saveDir = /tmp
          如果tmp文件夾不存在的話,會自動建立,一般在磁盤根目錄
          還有一個就是簡單的顯示上傳成功的頁面。
          <%@ page language="java"  pageEncoding="utf-8"%>
          <%@taglib prefix="s" uri="/struts-tags"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>  
              
          <title>上傳成功</title>
            
          </head>
            
            
          <body>
                上傳成功!
                
          <s:property value="title"/>
            
          </body>
          </html>

          posted on 2009-05-06 22:19 duduli 閱讀(2084) 評論(1)  編輯  收藏 所屬分類: SSH/SSH2

          評論

          # re: struts2的文件上傳 2012-01-10 23:34 11

          111  回復  更多評論   

          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地攤兒

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          @duduli
          主站蜘蛛池模板: 龙门县| 乐至县| 苍溪县| 奈曼旗| 嘉黎县| 建湖县| 石家庄市| 永仁县| 盖州市| 无棣县| 保山市| 乌恰县| 五家渠市| 新兴县| 错那县| 谢通门县| 肃南| 合作市| 焉耆| 英吉沙县| 固安县| 桑日县| 惠水县| 广水市| 黄浦区| 桓仁| 桂林市| 滨海县| 卓尼县| 金乡县| 台州市| 哈密市| 南安市| 岳普湖县| 新余市| 喀喇沁旗| 襄樊市| 米泉市| 溧水县| 富阳市| 湖南省|