使用注解完成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>

0 0
原创粉丝点击