使用Struts2标签实现文件上传

第一步:

    Jsp页面中:

       <%@taglib prefix="s" uri="/struts-tags" %>

   <tr>

          <td>图片</td>

          <tdclass="admin_righe_td"><s:filetheme="simple"name="myFile"></s:file></td>

       </tr>

       注意:

       theme=”simple”实现{图片}和上传文件部分不换行。

       此外,注意form中,

<formaction="news_add_admin.action" method="post"enctype="multipart/form-data" name="form1"id="form1">


第二步:  

     Action中:

publicclassNewsAction extends ActionSupport{

   private NewsServicenewsService = new NewsService();

   private IntegernewsId;

   private Newsnews;

  

   privatestatic final int BUFFER_SIZE = 1024 * 1024 * 5;

   // 实现文件上传

   private FilemyFile; // <s:file>标志不仅仅是绑定到myFile

   private StringcontentType; // 还有myFileContentType(上传文件的MIME类型)

   private StringfileName; // myFileFileName(上传文件的文件名,该文件名不包括文件的路径)

   private StringimageFileName;

  

   public File getMyFile() {

      returnmyFile;

   }

 

   publicvoid setMyFile(File myFile){

      this.myFile = myFile;

   }

 

   public String getMyFileContentType() {

      returncontentType;

   }

 

   publicvoid setMyFileContentType(StringcontentType) {

      this.contentType = contentType;

   }

 

   // 页面中使用myFileAction中也为myFile,注意getset名字中要有MyFile

   public StringgetMyFileFileName() {

      returnfileName;

   }

 

   publicvoidsetMyFileFileName(String fileName) {

      this.fileName = fileName;

   }

 

   public StringgetImageFileName() {

      returnimageFileName;

   }

 

   publicvoid setImageFileName(StringimageFileName) {

      this.imageFileName = imageFileName;

   }

  

   public NewsServicegetNewsService() {

      returnnewsService;

   }

 

   publicvoidsetNewsService(NewsService newsService) {

      this.newsService = newsService;

   }

  

   public Integer getNewsId() {

      returnnewsId;

   }

 

   publicvoid setNewsId(Integer newsId){

      this.newsId = newsId;

   }

 

   public News getNews() {

      returnnews;

   }

 

   publicvoid setNews(News news) {

      this.news = news;

   }

 

   private Stringurl;

  

   public String getUrl() {

      returnurl;

   }

 

   publicvoid setUrl(String url) {

      this.url = url;

   }

 

   public String newslist() {

      // Struts2获取request

      Maprequest = (Map) ActionContext.getContext().get("request");

      request.put("newsList",newsService.list());

//    ActionContext.getContext().put("newsList",newsService.list());

      returnSUCCESS;

   }

  

   public String add() {

      imageFileName =new Date().getTime() + getExtention(fileName);// 上传后,新文件名

      Stringpath = ServletActionContext.getServletContext().getRealPath("/images")+

            "/" + imageFileName;

      FileimageFile = newFile(path);

      copy(myFile, imageFile);

      news.setNewsImage("/images"+"/"+ imageFileName);

      newsService.add(news);

      //url

      //ActionContext.getContext().put("url","news_newslist_admin.action");

      this.setUrl("news_newslist_admin.action");

      return"redirect";

   }

  

   public String newsadd() {

      returnSUCCESS;

   }

  

   privatestatic void copy(File src, Filedest) {

      try {

         InputStreamin = null;

         OutputStreamout = null;

         try {

            in= newBufferedInputStream(new FileInputStream(src),BUFFER_SIZE);

            out= newBufferedOutputStream(new FileOutputStream(dest),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();

      }

   }

  

   privatestatic StringgetExtention(String fileName) {

      int pos =fileName.lastIndexOf(".");

      returnfileName.substring(pos);

   }

}

然后在struts.xml文件中进行配置。

小细节:

    若在配置文件中使用${url},则需要在相应的Action中,添加url的get和set方法,否则取不到url的值。${ url }在struts.xml中并不是el表达式,只是一种取值方式。

你可能感兴趣的:(文件上传,struts)