使用注解完成struts2的上传下载
来源:互联网 发布:linux下mysql安装教程 编辑:程序博客网 时间:2024/05/16 11:12
好久不写技术性的文章了,因为工作的需要,来实现了一下struts2的注解上传下载。关于在xml的配置文件里面的写法很常见,也就不再整理了。个人感觉还是使用注解比较的方便。废话不多说,上代码了。
因为是通过注解来实现的,所以我们的struts配置文件就不用动了(直接从ssh框架已经配置完成开始说的)。
action层代码:
上传代码:
@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 <s:file name="upload" ... /> the File itself will be
// obtained through getter/setter of <file-tag-name>
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 <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>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 <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>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;
}}
下载代码:
/**
* 所下载文件相关的的几个属性:文件格式 contentType,
* 获取文件名的方法inputName,
* 文件内容(显示的)属性contentDisposition,
* 限定下载文件 缓冲区的值bufferSize
* */
@Results({ @Result(name = "success", type = "stream", params = { "contentType","application/octet-stream", "inputName","inputStream", "contentDisposition","attachment;filename=‘${fileName}’", "bufferSize", "4096" }) })
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 8784555891643520648L;
private String STORAGEPATH = "/upload/Readme.txt";//下载文件的真实路径
private String fileName;// 初始的通过param指定的文件名属性
private String inputPath;// 指定要被下载的文件路径
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getInputStream() throws Exception {
/**
* 下载用的Action应该返回一个InputStream实例,
* 该方法对应在result里的inputName属性值为targetFile
**/
return ServletActionContext.getServletContext().getResourceAsStream(STORAGEPATH);
}
public String execute() throws Exception {
return SUCCESS;
}
}
jsp代码(简单的写一下,一般大家都会用插件的吧,这里只是用struts的标签来简单说明一下使用)
上传:
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="upload" label="File" />
<s:submit />
</s:form>
下载:
<s:a href="download.action">Download</s:a>
- 使用注解完成struts2的上传下载
- struts2 注解的使用
- struts2的注解使用
- 使用Struts2进行上传下载
- Struts2使用注解,完成请求响应,实现零配置
- struts2的文件上传下载
- Struts2图片的上传下载
- struts2的上传下载
- 使用Spring注解完成Bean的定义
- 使用Spring注解完成Bean的定义
- 使用注解完成@OneToOne关系的构建
- 使用自定义注解完成@Test注解功能类似的效果
- 使用Struts2简单的基本文件上传下载实现
- 【Struts2】(3)Struts2国际化、Struts2上传下载、Struts2标签使用
- struts2实现文件的上传下载
- 基于struts2的文件上传下载
- 第一次使用Struts2的注解功能
- Struts2.5.2简单的使用注解
- 一分钟学会将OpenStack Havana代码编译成DEB包
- UVa10878_Decode the tape(小白书字符串专题)
- 根据package name关闭进程
- zlog
- Jumping into C++ charpter2:The Basics of C++ 中文翻译
- 使用注解完成struts2的上传下载
- IOS之同步请求、异步请求、GET请求、POST请求
- oracle中动态SQL详解
- ini更改文件夹图标
- android手机通过USB共享电脑网络
- 在android 4.2.2上调试MU609步骤,WCDMA
- Linux 用export 声明临时环境变量失败的方法
- UVa644_Immediate Decodability(小白书字符串专题)
- coreseek搭配使用python源