struts2的文件上傳

          一直以來自己都是看書的時(shí)候多,而實(shí)際做卻很少,
          最近慢慢開始做東西了,發(fā)現(xiàn),看與做是2個(gè)完全不同的事情。
          記錄下面的東西是為了以后使用方便。
          利用struts2上傳文件。
          上傳頁(yè)面
                 <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è)就是簡(jiǎn)單的顯示上傳成功的頁(yè)面。
          <%@ 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 閱讀(2086) 評(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>

          搜索

          •  

          積分與排名

          • 積分 - 257428
          • 排名 - 220

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          @duduli
          主站蜘蛛池模板: 米易县| 体育| 抚州市| 靖西县| 察雅县| 蓬莱市| 嵊州市| 交口县| 胶州市| 靖西县| 九龙城区| 闸北区| 江陵县| 上思县| 陕西省| 罗山县| 雷山县| 策勒县| 沙湾县| 共和县| 奇台县| 深圳市| 雷波县| 安龙县| 衡南县| 乌拉特前旗| 利津县| 永靖县| 雷波县| 渑池县| 平和县| 诸暨市| 宜兰县| 海安县| 榆林市| 蓬莱市| 湖南省| 景宁| 绥滨县| 全州县| 华坪县|