您的位置:首页 > 编程语言 > Java开发

[Struts2] 文件上传和下载

2017-09-07 20:33 375 查看

表单准备

要想使用HTML表单上传一个或多个文件:

需把HTML表单的enctype属性设置为multipart/form-data

需把HTML表单的method属性设置为post

需添加
<input type="file">
字段

Struts对文件上传的支持

在Struts应用程序里,FileUpload拦截器和Commons FileUpload组件可以完成文件的上传。

步骤:

在jsp页面的文件上传表单里使用file标签,如果需要一次上传多个标签,就必须使用多个file标签,但他们的名字必须是相同的

在Action中新添加3个和文件上传相关的属性,提供对应的getter和setter方法,这3个属性的名字必须是以下格式:

[FileName]:File-被上传的文件,例如data

[FileName]ContentType:String-上传文件的文件类型,例如dataContentType

[FileName]FileName:String-上传文件的文件名,例如dataFileName

如果上传多个文件,可以使用List

文件上传主要有以下一些知识点:

Struts2的文件上传实际上使用的是Commons FileUpload组件,所以需要导入:

commons-fileupload-1.3.jar

commons-io-2.0.1.jar

Struts2进行文件上传需要使用FileUpload拦截器

使用IO流进行文件的上传即可

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(ppt);
System.out.println(pptContentType);
System.out.println(pptFileName);
System.out.println(pptDesc);

ServletContext servletContext = ServletActionContext.getServletContext();
String dir = servletContext.getRealPath("./" + pptFileName);
System.out.println(dir);
FileOutputStream out = new FileOutputStream(dir);
FileInputStream in = new FileInputStream(ppt);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
return super.execute();
}


一次传多个文件

若传递多个文件,则上述的3个属性可以改为List类型,多个文件域的name属性值需要一致。

upload.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<s:form action="testUpload" method="post" enctype="multipart/form-data">
<s:file name="ppt" label="PPTFile"></s:file>
<!--为了使得textField回显不出问题,这里需要使用下标,因为pptDesc是List类型-->
<s:textfield name="pptDesc[0]" label="PPTDesc"></s:textfield>
<s:file name="ppt" label="PPTFile"></s:file>
<s:textfield name="pptDesc[1]" label="PPTDesc"></s:textfield>
<s:file name="ppt" label="PPTFile"></s:file>
<s:textfield name="pptDesc[2]" label="PPTDesc"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 打开静态方法的限制 -->
<package name="default" namespace="/" extends="struts-default">
<action name="testUpload" class="com.glemontree.struts.UploadAction" method="execute">
<result>/success.jsp</result>
<!-- 回显 -->
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>


可以对文件的大小、类型、扩展名等进行限制,可以通过配置FileUploadInterceptor拦截器的参数的方式来进行限制,有以下几个参数:

maximumSize(上传的单个文件的大小,单位是字节,默认最大值为2M)

allowedTypes(允许的类型,使用逗号分隔)

allowedExtensions(允许的上传文件扩展名,多个使用逗号分隔)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor-stack name="glemontreestack">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2000</param>
<param name="fileUpload.allowedTypes">text/html,text/xml</param>
<param name="fileUpload.allowedExtensions">html,dtd,xml</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 配置使用这个拦截器栈 -->
<default-interceptor-ref name="glemontreestack"></default-interceptor-ref>
<action name="testUpload" class="com.glemontree.struts.UploadAction" method="execute">
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>


Struts文件下载

Struts2中使用type=”stream”的result进行下载即可

具体使用细节请查看Struts2文档

可以为stream的result设定如下参数:

contentType - the stream mime-type as sent to the web browser (default =
text/plain
).

contentLength - the stream length in bytes (the browser displays a progress bar).

contentDisposition - the content disposition header value for specifing the file name (default =
inline
, values are typically attachment;filename=”document.pdf”.(设定Content-Disposition响应头,该响应头指定是一个文件下载类型,一般取值为attachment;filename=”document.pdf”)

inputName - the name of the InputStream property from the chained action (default =
inputStream
).(指定文件输入流的getter定义的那个属性的名字,默认为inputStream)

bufferSize - the size of the buffer to copy from input to output (default =
1024
).

allowCaching if set to ‘false’ it will set the headers ‘Pragma’ and ‘Cache-Control’ to ‘no-cahce’, and prevent client from caching the content. (default =
true
)

contentCharSet if set to a string, ‘;charset=value’ will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

前面三个成员的值可以通过在Action中动态指定,后面三个的值可以在struts.xml文件中静态指定。

以上参数可以在Action中以getter方法的方式提供,动态指定参数的值

举例

struts.xml

<action name="testDownload" class="com.glemontree.struts.DownLoadAction">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action>


DownloadAction.java

package com.glemontree.struts;

import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

private String contentType;
private long contentLength;
private String contentDisposition;
private InputStream inputStream;

public String getContentType() {
return contentType;
}

public long getContentLength() {
return contentLength;
}

public String getContentDisposition() {
return contentDisposition;
}

public InputStream getInputStream() {
return inputStream;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
// 确定各个成员变量的值
contentType = "text/html";
contentDisposition = "attachment;filename=hidden.html";
ServletContext servletContext = ServletActionContext.getServletContext();
String fileName = servletContext.getRealPath("/hidden.html");
inputStream = new FileInputStream(fileName);
contentLength = inputStream.available();
return super.execute();
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息