在Struts2中借助Commons FileUpload组件很容易实现文件的上传与下载,Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中,从而我们能够以本地文件方式操作浏览器上传文件。但是这些例子大多需要在struts.xml中进行配置,比较麻烦,好在Struts2中提供了struts2-convention-plugin插件,可以在程序中使用注解对Action进行配置,使之更加灵活。
下面介绍使用注解实现文件的上传:
1. 添加依赖的jar包
2. 配置struts.xml文件,这里的struts文件基本上不需要配置,如下即可:
3. 编写文件上传的Action:FileUploadAction.java
package com.figo.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Action("fileUpload")
@InterceptorRefs(value = { @InterceptorRef("fileUploadStack") })
@Results({ @Result(name = "success", location = "/result.jsp") })
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
// 封装上传文件域的属性
private File upload;
// 封装上传文件类型的属性
private String contentType;
// 封装上传文件名的属性
private String fileName;
private String storageFileName;
// private String storagePath;
// since we are using the File itself will be
// obtained through getter/setter of
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
// since we are using the file name will be
// obtained through getter/setter of FileName
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
public String getStorageFileName() {
return storageFileName;
}
public void setStorageFileName(String storageFileName) {
this.storageFileName = storageFileName;
}
// since we are using the content type will be
// obtained through getter/setter of ContentType
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
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();
}
}
public static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() throws Exception {
// storageFileName = new Date().getTime() + getExtention(fileName);
storageFileName = fileName;
System.out.println("FileName: " + fileName);
System.out.println("ContentType: " + contentType);
System.out.println("File: " + upload);
File storageFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + storageFileName);
copy(upload, storageFile);
return SUCCESS;
}
}
FileUploadAction作用是将浏览器上传的文件拷贝到WEB应用程序的upload文件夹下。
4. 编写文件上传的表单页面 upload.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
File Upload
将表单的提交方式设为POST,然后将enctype设为multipart/form-data, 5. 结果返回页面 result.jsp:
<%@ page language="java" contentType="text/html;charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
File upload success
File
upload success.
Saved as
File:
ContentType:
6. 配置web.xml:
Upload
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
actionPackages
com.figo.action
struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
struts2
/*
index.html
index.htm
index.jsp
重点是在filter里添加
7. 页面测试:
文件上传页面:
文件上传返回结果:
因为是在eclipse工程里进行的测试,所以文件保存在了临时文件里,如果是真正发布的话,上传的文件就会保存到预先设置好的目录中。
注:
1. struts2默认最大只能上传2M的文件,可以在struts.xml中扩大默认文件上传的大小。
2. 总结常用的注解如下:
Namespace:指定命名空间。
ParentPackage:指定父包。
Result:提供了Action结果的映射(一个结果的映射)。
Results:“Result”注解列表。
ResultPath:指定结果页面的基路径。
Action:指定Action的访问URL。
Actions:“Action”注解列表。
ExceptionMapping:指定异常映射(映射一个声明异常)。
ExceptionMappings:一级声明异常的数组。
InterceptorRef:拦截器引用。
InterceptorRefs:拦截器引用组。
这里只是给大家起个抛砖引玉的效果,程序本身很简单,主要是配置,下一篇介绍零配置文件下载。
源代码下载链接:http://download.csdn.net/detail/sxwyf248/4462899