Struts2学习笔记16:Struts2的文件上传和下载【续】三

来源:互联网 发布:mac 应用快捷键 编辑:程序博客网 时间:2024/05/22 06:32
 

Struts2学习笔记16:Struts2的文件上传和下载【续】三

第十五讲

学习内容:

1)编写JavaScript代码实现上传任意多个文件

2)限制上传文件类型和大小

3)自定义提示信息

4)下载文件

 1)编写JavaScript代码实现上传任意多个文件

使用表格布局表单,设置上传文件元素所处单元格的ID为fileID,在上传文件元素后,加上一个按钮与以及该按钮的单击事件。注意:将s:form的theme属性设置为“theme”,代码如下:

<td id="fileID">

<s:file name="file"></s:file>

<input type="button" name="button" value="添加"

 onclick="addMore();" />

</td>

<script type="text/javascript">

function addMore(){

var td = document.getElementById("fileID");

var br = document.createElement("br");

var file = document.createElement("input");

var button = document.createElement("input");

file.type = "file";

file.name = "file";

button.type = "button";

button.name = "remove";

button.value="删除";

button.onclick = function(){

td.removeChild(br);

td.removeChild(file);

td.removeChild(button);

}

td.appendChild(br);

td.appendChild(file);

td.appendChild(button);

}

</script>

此功能只需修改该JSP页面的代码就可以了。

 2)限制上传文件类型和大小

需要用到类的位置

Struts2-core-2.0.11.2.jar

org.apache.struts2.interceptor/FileUploadInterceptor

maximumSize设置允许上传最大文件的大小

allowedTypes设置允许上传的文件类型

方法:在action中添加"interceptor-ref"

<action name="upload" class="upload.UploadAction">

<result name="success">/uploadresult.jsp</result>

<result name="input">/input.jsp</result>

<interceptor-ref name="fileUpload">

<param name="maximumSize">1048576</param>  

<!-- 允许单个文件最大大小 -->

<param name="allowedTypes">text/plain</param> 

<!-- 允许上传类型 -->

</interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

</action>

注意:

对"allowedTypes"不是填写文件的后缀名,在tomcat的conf文件下打开web.xml,找到mime-mapping元素

    <mime-mapping>

        <extension>txt</extension>

        <mime-type>text/plain</mime-type>

    </mime-mapping>

 txt 文件的后缀名

 text/plain 文件类型

即:对于*.txt类型文件allowedTypes的value为text/plain

 3)自定义提示信息

示例文件存放位置:

Struts2-core-2.0.11.2.jar

Org.apache.struts2/struts2-message_da.properties

 1.在struts.xml的struts元素中添加

<constant name="struts.custom.i18n.resources" value="message">

</constant>

 2.在src目录中建立message.properties,添加代码:

struts.messages.error.file.too.large=<a href="/struts17/upload.jsp">/u4e0a/u4f20/u6587/u4ef6/u592a/u5927/uff0c/u8bf7/u91cd/u65b0/u4e0a/u4f20</a>

struts.messages.error.content.type.not.allowed=<a href="/mstruts17/upload.jsp">/u4e0a/u4f20/u6587/u4ef6/u7c7b/u578b/u4e0d/u7b26/uff0c/u8bf7/u91cd/u65b0/u4e0a/u4f20</a>

native2ascii进行的Unicode转换编码

 3.运行

 4)下载文件

 1.建立包名 download 建立 DownloadAction.java文件,继承

ActionSupport类,代码如下:

public class DownloadAction extends ActionSupport {

private static final long serialVersionUID =

 1926631455637861510L;

public InputStream getDownloadFile() {

return ServletActionContext.getServletContext().getResourceAsStream(

"/temp/ExamplePrograms.ZIP");

}

@Override

public String execute() throws Exception {

return SUCCESS;

}

}

 2.在temp下放入ExamplePrograms.ZIP文件

 3.建立download.jsp文件,部分相关代码:

<%@ taglib prefix="s" uri="/struts-tags" %>

…………

<s:a href="/struts17/down.action">download文件</s:a>

 4.配置struts.xml文件

<action name="down" class="download.DownloadAction">

<result name="success" type="stream">

<param name="contentType">application/zip</param>

<param name="contentDisposition">filename="ExamplePrograms.ZIP"</param>

<param name="inputName">downloadFile</param>

</result>

</action>

contentType

文件类型

contentDispostion

文件名,格式固定

inputName

输入流名称对应于getDownloadFile

,小写字母“d”

 5.运行

原创粉丝点击