java中struts2实现简单的文件上传与下载_Java教程-查字典教程网
java中struts2实现简单的文件上传与下载
java中struts2实现简单的文件上传与下载
发布时间:2016-12-28 来源:查字典编辑
摘要:一、Struts2文件上传Struts2的文件上传实现非常简单,只需要简单几步就可完成;注意:(1)文件上传的struts2标签为:(2)文...

一、Struts2文件上传

Struts2的文件上传实现非常简单,只需要简单几步就可完成;

注意:

(1)文件上传的struts2标签为:<s:file name="" label="上传"/>

(2)文件上传的前提是表单属性method="post" enctype="multipart/form-data";

(3)web应用中必须包含common-fileupload.jar和common-io.jar,因为struts2默认上传解析器使用的是jakarta;

(4)可以在struts.xml中配置最大允许上传的文件大小:<constant name="struts.multipart.maxSize" value="....."/>,默认为2M;

1.普通文件上传

实现规则:

(1)在JSP中设定表单控件<s:file name="upload" label="上传"/>

(2)在Action中定义属性:

private File upload; //包含文件内容

private String uploadFileName; //上传文件的名称;

private String uploadContentType; //上传文件的MIME类型;

这些属性都会随着文件的上传自动赋值;

(3)在execute()中完成写入磁盘功能;

代码示例:

Upload01Action.java

package org.upload.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class Upload01Action extends ActionSupport { private File upload; private String uploadFileName; private String uploadContentType; private String name; public String execute()throws Exception{ String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload"); String filename = path+File.separator+name; FileInputStream in = new FileInputStream(upload); FileOutputStream out = new FileOutputStream(filename); byte[]b = new byte[1024]; int len = 0; while((len=in.read(b))>0){ out.write(b,0,len); } out.close(); return SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

struts.xml

<"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> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="message"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="upload0*"> <param name="name">1.jpg</param> <result>/{1}.jsp</result> </action> </package> </struts>

1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP '1.jsp' starting page</title> </head> <body> <s:form action="upload01" method="post" enctype="multipart/form-data"> <s:file name="upload" label="上传"></s:file> <s:submit value="上传"></s:submit> </s:form> </body> </html>

2.利用拦截器进行过滤

手动实现过滤的方式非常简单,就是利用输入校验的方式进行过滤,即在validate()中进行过滤;

而这里要讲的拦截器方式是很好的方式,只需要在配置文件中配置,灵活性很好,能够限制文件的类型、文件的大小;如果上传的文件不符合要求,则返回input逻辑视图;

配置拦截器步骤:

(1)文件上传的拦截器为fileUpload;

(2)需要给定参数allowedTypes、maximumSize;

(3)在fileUpload拦截器后,需要添加<interceptor-ref name="defaultStack"/>

代码示例:

由于通过拦截器进行过滤只需要配置struts.xml,因此这里只给出struts.xml的配置

<"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> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="message"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="upload0*"> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg,image/gif</param> <param name="maximumSize">1024*1024</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <param name="name">1.jpg</param> <result>/{1}.jsp</result> <result name="input">/{1}.jsp</result> </action> </package> </struts>

我们还需要配置文件上传失败后的错误提示信息,我们需要在全局国际化资源文件中配置:

struts.messages.error.content.type.not.allowed=文件类型不匹配 struts.messages.error.file.too.large=文件太大

二、Struts2文件下载

我们在学习Servlet和HTTP协议时已经可以实现文件下载,即写content-disposition头即可,struts2的实现原理也是这个,但是提供了更好的封装性;

struts2的stream结果类型专门用于实现文件下载;

(1)struts.xml中配置stream结果类型,并配置contentType、contentDisposition、bufferSize参数即可,模板:

<action name="download"> <result type="stream" name="success"> <param name="contentType"></param> <param name="contentDisposition">attachment;filename=""</param> <param name="bufferSize">4096</param> </result> </action>

(2)在Action中创建public InputStream getInputStream()throws Exception;方法,此方法用于获得下载文件的输入流;

DownloadAction.java

package org.download.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { public InputStream getInputStream()throws Exception{ return ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/upload/1.jpg"); } public String execute()throws Exception{ return SUCCESS; } }

struts.xml

<action name="download"> <result type="stream" name="success"> <param name="contentType">image/jpeg</param> <param name="contentDisposition">attachment;filename="1.jpg"</param> <param name="bufferSize">4096</param> </result> </action>

就可以完成下载。

java中struts2文件上传下载的实现就为大家介绍到这里,谢谢大家的阅读。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Java学习
    热门Java学习
    编程开发子分类