« 【分享】Solaris在Intel... | Main | 如何给JPanel加上背景图片 »
http://developers.sun.com.cn/blog/functionalca/date/20090429 星期三 2009年04月29日

Struts2 文件上传简介

Struts2 文件上传简介

作者:曹祺
Blog: http://blogs.sun.com/greysh
Web: http://www.greysh.com
Email: Qi.Cao@Sun.com
本文链接:http://developers.sun.com.cn/blog/functionalca/entry/struts2_%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E7%AE%80%E4%BB%8B
源代码下载:
http://developers.sun.com.cn/blog/functionalca/resource/Greysh/upload.part1.rar
http://developers.sun.com.cn/blog/functionalca/resource/Greysh/upload.part2.rar 
 
http://developers.sun.com.cn/blog/functionalca/resource/Greysh/upload.part3.rar
http://developers.sun.com.cn/blog/functionalca/resource/Greysh/upload.part4.rar  
难度:入门

1.简介
2.环境配置
3.代码与Helloworld

1.
Struts2将文件上传完全整合上去了,采取的上传模式是一种契约式,也就是只要按照指定的命名方法边可以上传,核心程序仅仅只有FileUtils.copyFile(excelImage, target);边可以将excellImage传到指定的targer.如果用传统的JSP上传,不仅要控制很多参数还可能用第三方上传控件,例如JSP Smart.而且用s:file可以进行文件后缀校验,很方便.

2.
环境配置如下:
需要的jar包如下
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
l.txt
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar

commons-fileupload-1.2.1.jar:控制文件上传
commons-io-1.3.2.jar:控制文件IO
commons-logging-1.0.4.jar:日志
freemarker-2.3.13.jar: Freemarker是struts2默认的模版语言
ognl-2.6.11.jar: Struts2默认的表达式语言OGNL:对象图形化导航语言
struts2-core-2.1.6.jar:  Struts2核心包
xwork-2.1.2.jar: 也是Struts2核心包,毕竟struts2很大部分是来自webwork

这几个包是必须得包,一个都不能少,然后web.xml的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
 <welcome-file-list>
  <welcome-file>fileUpload.jsp</welcome-file>
 </welcome-file-list>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>
然后配置struts2
<?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>
 <include file="struts-default.xml" />
 <include file="struts_image.xml" />
</struts>
这样,整体的配置就OK了

3.代码实现和helloworld

本教程是demo,故上传页面
<%@ 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>File Upload</title>
 </head>
 <body>
  <s:form action="/image/image!addImage.action" method="post"
   enctype="multipart/form-data">
   <s:file name="excelImage" theme="simple" />
   <s:submit name="submit" value="Upoad" theme="simple" />
  </s:form>
 </body>
</html>
上传后提交到得action

package com.greysh.action;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport {

 private static final long serialVersionUID = -382362366152017940L;
 
    private File excelImage;// 实际上传文件

    private String excelImageContentType; // 文件的内容类型

    private String excelImageFileName; // 上传文件名
 
 public String addImage() throws SQLException, IOException {
   String targetDir = ServletActionContext.getServletContext()
     .getRealPath("/upload");
   File target = new File(targetDir, excelImageFileName);
   FileUtils.copyFile(excelImage, target);
   System.out.println(excelImageFileName);
   return "success";
 }

 public File getExcelImage() {
  return excelImage;
 }

 public void setExcelImage(File excelImage) {
  this.excelImage = excelImage;
 }

 public String getExcelImageContentType() {
  return excelImageContentType;
 }

 public void setExcelImageContentType(String excelImageContentType) {
  this.excelImageContentType = excelImageContentType;
 }

 public String getExcelImageFileName() {
  return excelImageFileName;
 }

 public void setExcelImageFileName(String excelImageFileName) {
  this.excelImageFileName = excelImageFileName;
 }


}
这里采用的契约,也就是说,你的字段文件名是excelImage,则必须添加excelImageContentType和excelImageFileName。然后为了页面能访问,需要添加get和set方法,由于struts2自身带IoC和AOP功能,故不需要实例化。这样提交后边可以了,他会根据字段名去处理的。

还要配置struts2的action

<?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>
 <package name="image" extends="struts-default" namespace="/image">
  <action name="image"
   class="com.greysh.action.FileAction">
   <result name="success">/fileSuccess.jsp</result>
  </action>
 </package>
</struts>



发表于 Sun Functional 校园大使 [JavaEE] ( 四月 29, 2009 10:19 下午 ) Permalink | 评论[0]
评论:

发表一条评论:
  • HTML语法: 禁用