struts2的文件上傳

          一直以來自己都是看書的時(shí)候多,而實(shí)際做卻很少,
          最近慢慢開始做東西了,發(fā)現(xiàn),看與做是2個(gè)完全不同的事情。
          記錄下面的東西是為了以后使用方便。
          利用struts2上傳文件。
          上傳頁面
                 <form action="upload" method="post" enctype="multipart/form-data">
                     文件標(biāo)題:
          <input type="text" name="title" /><br>
                     選擇文件:
          <input type="file" name="upload" /><br>
                     
          <input value="上傳" type="submit" />
                 
          </form>
          然后就是struts.xml文件,在  <package>標(biāo)簽中
                  <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的配置文件得到上傳目錄,這個(gè)是很重要的
              @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";
              }
          }

          如果你認(rèn)為配置這些就可以了,那你就錯(cuò)了,還需要配置一個(gè)struts.properties文件。定義一個(gè)臨時(shí)的上傳文件夾。
          #配置臨時(shí)存放目錄
          struts.multipart.saveDir = /tmp
          如果tmp文件夾不存在的話,會(huì)自動(dòng)建立,一般在磁盤根目錄
          還有一個(gè)就是簡單的顯示上傳成功的頁面。
          <%@ 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 閱讀(2081) 評(píng)論(1)  編輯  收藏 所屬分類: SSH/SSH2

          評(píng)論

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

          111  回復(fù)  更多評(píng)論   

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

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團(tuán)隊(duì)

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地?cái)們?/h3>

          搜索

          •  

          積分與排名

          • 積分 - 256304
          • 排名 - 220

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          @duduli
          主站蜘蛛池模板: 建昌县| 苏州市| 丰原市| 芷江| 桓台县| 云南省| 怀远县| 郯城县| 湘阴县| 柳江县| 樟树市| 卓资县| 稷山县| 大庆市| 孝义市| 灵宝市| 尚志市| 台湾省| 广饶县| 嘉义市| 清徐县| 黔江区| 沧州市| 会昌县| 板桥市| 宣城市| 吉首市| 武乡县| 海宁市| 元谋县| 临沧市| 五原县| 海林市| 无为县| 巴林左旗| 邵阳县| 东平县| 林口县| 恭城| 贵南县| 周至县|