
首先了解几个术语:
MIME类型:设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。是描述消息内容类型的因特网标准。MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。Tomcat的安装目录\\conf\\web.xml 中就定义了大量MIME类型。
:是Struts 2的一个强有力的工具,有许多功能(feature)都是构建于它之上,如国际化、转换器,校验等。它是AOP思想的编程方式。提供一种机制使开发者能把相对的代码抽象出来,配置到Action前后执行。
准备相应的jar包:commons-fileupload-1.2.1.jar;commons-io-1.4.jar;commons-logging-1.0.4.jar;freemarker-2.3.8.jar;ognl-2.6.11.jar;struts2-core-2.0.14.jar;xwork-2.0.7.jar。
接下来编写一个Action名字叫做UploadAction,继承自ActionSupport虽然struts2的Action抛弃了request,response等Servlet API,但我们仍然可以通过ServletActionContext来得到相关Servlet对象,比如request,response,application,session等。
Java代码
1package org.common.action;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6
7import org.apache.struts2.ServletActionContext;
8
9import com.opensymphony.xwork2.Action;
10import com.opensymphony.xwork2.ActionContext;
11import com.opensymphony.xwork2.ActionSupport;
12
13@SuppressWarnings("all")14public class UploadAction extends ActionSupport {
15private File attach;
16private String attachContentType;
17private String attachFileName;
18
19public String upload() throws Exception {
20File saved = new
File(ServletActionContext.getServletContext().getRealPath("uploads"), attachFileName);
21FileInputStream fis = null;
22FileOutputStream fos = null;
23try {
24saved.getParentFile().mkdirs();
25fis = new FileInputStream(attach);
26fos = new FileOutputStream(saved);
27byte[] bytes = new byte[1024];
28int len = 0;
29while ((len=fis.read(bytes))!=-1) {
30fos.write(bytes, 0, len);
31}
32
3334} catch (Exception e) {
35 e.printStackTrace();
36} finally {
37if (fis!=null) {
38fis.close();
39}
40if (fos!=null) {
41fos.close();
42}
43}
44return Action.SUCCESS;
45}
46
47public File getAttach() {
48return attach;
49}
50
51public void setAttach(File attach) {
52this.attach = attach;
53}
54
55public String getAttachContentType() {56return attachContentType;
57}
58
59public void setAttachContentType(String attachContentType) {
60this.attachContentType = attachContentType;
61}
62
63public String getAttachFileName() {
return attachFileName;
65}
66
67public void setAttachFileName(String attachFileName) {
68this.attachFileName = attachFileName;
69}
70
71}
配置struts.xml文件,注意package继承struts-default的相关配置,在使用的时候,在Action 里面最后一定要引用struts2自带的缺省堆栈defaultStack,不然会出错。
Xml代码
72
7374"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"75"http://struts.apache.org/dtds/struts-2.0.dtd">
76
77 78 79 80 81 82 83 84 85 86 87 image/bmp, image/x-png, image/gif, image/jpeg 90204800 91 92 94 97
上传文件的jsp页面(注意file的name属性一定要与Action里面的File类型属性名一致):
Html代码
98
99 100 101 102 103
104
105
上传成功的jsp页面:
Html代码
106<%@ page contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
107<%@ taglib uri="/struts-tags" prefix="s" %>
108 109
110
111
112/>
113
116
117
118
120
总结一下:导入文件上传相关的两个组件commons-fileupload.jar和commons-io.jar,两者缺一不可;前台form表单设置enctype="multipart/form-data",file标签的name属性要与相关Action 里的File属性名一致;后台定义上传Action,该Action可以定义文件的原始名称xxx与file的那么属性一致,struts会将原文件名设置到该属性上,此外还可以设置xxxContentType和xxxFileName属性,只要设置了这些都要提供相关的getter和setter方法供struts使用;根据需要使用fileUpload,从而可以上传文件大小和文件类型,注意上传文件大小需要在struts.multipart.maxSize属性要大于的maximumSize属性,否则maximum参数不起作用;显示使用了后,一定要将
private File[] attach; private String[] attachContentType; private String[] attachFileName;
