10.struts2的上传和下载

1.单文件上传

   上传文件对表单的要求

  1.  表单的提交方式为post
  2.  表单的enctype是multipart/form-data
  3.  表单中要有file类型的input文本域

Struts上传也是基于拦截器,底层还是使用commons-fileupload组件

Struts上传的步骤

1.建立表单

<form action="/struts2/upload/zhang" method="post" enctype="multipart/form-data">
	姓名:<input type="text" name="username"><br>
	文件:<input type="file" name="upload"><br>
	<input type="submit">
</form>

2.创建Action

注意:

接收的文件属性:和表单中文本域file类型input的name一致

接收的文件名字:file的name+FileName固定写法

接收的文件MIME类型:file的name+ContentType

 

public class UploadAction extends ActionSupport {
private String username;
	
	/**
	 * 要接收的文件,命名需要和表单中的file类型的input的name一致
	 */
	private File upload;
	
	/**
	 * 文件名的接收 File属性名FileName:固定写法
	 */
	private String uploadFileName;
	
	/**
	 * 获得上传文件的MIME类型,File属性名字ContentType:固定写法
	 */
	private String uploadContentType;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	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 upload() throws Exception{
		//获得servletContext
		ServletContext sc = ServletActionContext.getServletContext();
		//获得服务的绝对路径
		String realPath = sc.getRealPath("/");
		realPath = realPath + "upload\\"+uploadFileName;
		//注释的代码等价于这一行
		FileUtils.copyFile(upload, new File(realPath));
		/*//定义输入输出流
		InputStream in = new FileInputStream(upload);
		OutputStream out = new FileOutputStream(realPath);
		int len = -1;
		byte[] bs = new byte[1024];
		while((len = in.read(bs)) != -1){
			out.write(bs, 0, len);
		}
		out.close();
		in.close();*/
		return super.SUCCESS;
	}
}

struts.xml(注意webcontent下建立一个upload目录)

<struts>
	<!-- 
		开发模式
	 -->
	<constant name="struts.devMode" value="true"></constant>
	<package name="upload" extends="struts-default" namespace="/upload">
		
		<action name="zhang" class="com.zy.action.UploadAction" method="upload">
			<result name="success">/success.jsp</result>
			<result name="input">/form.jsp</result>
		</action>
	</package>
</struts>

 最后上传在服务器的upload里面

F:\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\struts2\upload

2.文件上传类型的验证

在上传时要验证文件类型和大小

<struts>
	<!-- 
		开发模式
	 -->
	<constant name="struts.devMode" value="true"></constant>
	<package name="upload" extends="struts-default" namespace="/upload">
		
		<action name="zhang" class="com.zy.action.UploadAction" method="upload">
			<!-- 主动引用默认拦截器栈 -->
			<interceptor-ref name="defaultStack">
			<!-- 设置上传拦截器fileUpload.allowedExtensions -->
			<param name="fileUpload.allowedExtensions">.jpg,.txt</param>
			</interceptor-ref>
			<result name="success">/success.jsp</result>
			<result name="input">/form.jsp</result>
		</action>
	</package>
</struts>

form.jsp页面加入<%@ taglib uri="/struts-tags" prefix="s"%>标签,body里面<s:fielderror></s:fielderror>负责提示错误

如下上传png错误

 

3.文件上传大小的验证

Struts2默认的上传文件的配置在default.properties的文件中

由struts.multipart.maxSize=2097152控制

 

 

如何改成中文呢?

我们发现展示并不是中文,我们使用国际化来处理

文件超过最大值的提示信息是在struts_message.propertise中,我们只要通过国际化文件覆盖它即可

这种错误属于<s:actionerror/>的错误,在jsp页面加<s:actionerror/>即可提示错误信息。

创建国际化文件 

 

<constant name="struts.custom.i18n.resources" value="com/zy/action/resource/msg"></constant>

 

然后再struts.xml中加载资源文件

<!-- 设置上传文件的大小 -->
	<constant name="struts.multipart.maxSize" value="1048576"></constant>


 

4.多文件的上传

多文件上传Action中把File属性以及和file属性相关的上传文件都变成数组,在execute中循环上传即可

package com.zy.action;


public class UploadAction extends ActionSupport {
private String username;
	
	/**
	 * 要接收的文件,命名需要和表单中的file类型的input的name一致
	 */
	private File[] upload;
	
	/**
	 * 文件名的接收 File属性名FileName:固定写法
	 */
	private String [] uploadFileName;
	
	/**
	 * 获得上传文件的MIME类型,File属性名字ContentType:固定写法
	 */
	private String [] uploadContentType;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/*set get省略*/

	public String upload() throws Exception{
		//获得servletContext
		ServletContext sc = ServletActionContext.getServletContext();
		//获得服务的绝对路径
		
		for(int i = 0; i < upload.length; i++){
			String realPath = sc.getRealPath("/");
			realPath = realPath + "upload\\"+uploadFileName[i];
			FileUtils.copyFile(upload[i], new File(realPath));
		}
		
		return super.SUCCESS;
	}
}

 form1.jsp

<body>
<s:fielderror></s:fielderror>
<s:actionerror/>
<form action="/struts2/upload/zhangs" method="post" enctype="multipart/form-data">
	姓名:<input type="text" name="username"><br>
	文件1:<input type="file" name="upload"><br>
	文件2:<input type="file" name="upload"><br>
	文件3:<input type="file" name="upload"><br>
	<input type="submit">
</form>

</body>

strust.xml

<action name="zhangs" class="com.zy.action.UploadAction" method="upload">
			<!-- 主动引用默认拦截器栈 -->
			<interceptor-ref name="defaultStack">
			<!-- 设置上传拦截器fileUpload.allowedExtensions -->
			<param name="fileUpload.allowedExtensions">.jpg,.txt,.MP4</param>
			</interceptor-ref>
			<result name="success">/success.jsp</result>
			<result name="input">/form.jsp</result>
		</action>

 

 5.文件的下载

Struts文件下载对动作类Action的要求:

在Action之中必须提供三个属性:

//提供一个输入流的属性,名字叫inputStream固定

        private InputStream inputStream;

         //定义文件的大小

         private int filelength;

         //定义文件名

         private String fileName;

action

public class DownAction extends ActionSupport {
	private InputStream inputStream;
	
	private int filelength;
	
	private String fileName;

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public int getFiellength() {
		return filelength;
	}

	public void setFiellength(int fiellength) {
		this.filelength = fiellength;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public String down() throws Exception{
		ServletContext sc = ServletActionContext.getServletContext();
		String realPath = sc.getRealPath("/upload/1.jpg");
		//实例化输入流
		inputStream = new FileInputStream(realPath);
		//给fileName赋值
		fileName = FilenameUtils.getName(realPath);
		fileName = URLEncoder.encode(fileName,"UTF-8");
		//给文件大小复制
		filelength = inputStream.available();
		return super.SUCCESS;
	}
}

struts.xml

<constant name="struts.devMode" value="true"></constant>
	<!-- 设置上传文件的大小 -->
	<constant name="struts.multipart.maxSize" value="1048576"></constant>
	<package name="down" extends="struts-default" namespace="/down">
		
		<action name="zhang" class="com.zy.action.DownAction" method="down">
			<result name="success" type="stream">
				<!-- 指定action中的输入流变量 -->
				<param name="inputName">inputStream</param>
				<!-- 设置响应的信息头content-disposition -->
				<param name="contentDisposition">attachment;filename=${fileName}</param>
				<!-- 使用下载的方式来返回结果 -->
				<param name="contentType">application/octet-stream</param>
				<!-- 配置文件的大小 -->
				<param name="contentLength">${filelength}</param>
			</result>
		</action>
	
	</package>

 

访问http://localhost:8080/struts2/down/zhang

跳转http://localhost:8080/struts2/down/down

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值