struts2学习之文件上传

上传html页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>写心情</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
  </head>
  
  <body style="font-size:14px;">
    <h2>写微博      <a href="index.jsp">返回首页</a></h2>
    <form action="upload" method="post" enctype="multipart/form-data">
    	<textarea name="sayMessage" rows="4" cols="50"></textarea><br>
    	发文件<input name="upload" type="file"/><input type="submit" value="发微博">
    </form>
    
  </body>
</html>


上传action:

package com.gifer.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpLoadAction extends ActionSupport {

	private static final long serialVersionUID = -1232100382633301276L;

	private static final Logger log = Logger.getLogger(FileUpLoadAction.class);

	// 微博内容
	private String sayMessage;

	// 上传多个文件的集合文本
	private List<File> upload;

	// 多个上传文件的类型集合
	private List<String> uploadContextType;

	// 多个上传文件的文件名集合
	private List<String> uploadFileName;

	// 文件存放路径根目录
	private String savePath;

	public String getSayMessage() {
		return sayMessage;
	}

	public void setSayMessage(String sayMessage) {
		this.sayMessage = sayMessage;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContextType() {
		return uploadContextType;
	}

	public void setUploadContextType(List<String> uploadContextType) {
		this.uploadContextType = uploadContextType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	@Override
	public String execute() throws Exception {
		// 把上传的文件放到指定的路径下
		// savePath = ServletActionContext.getServletContext().getRealPath(
		// "/WEB-INF/uploadfiles");
		savePath = ServletActionContext.getServletContext().getRealPath(
				savePath);

		// 写到指定的路径中
		File file = new File(savePath);
		// 如果指定的路径没有就创建
		if (!file.exists()) {
			file.mkdirs();
		}

		// 把得到的文件的集合通过循环的方式读取并放在指定的路径下
		for (int i = 0; i < upload.size(); i++) {
			try {
				// list集合通过get(i)的方式来获取索引
				FileUtils.copyFile(upload.get(i),
						new File(file, uploadFileName.get(i)));
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
		return SUCCESS;
	}

}


struts.xml配置:

<action name="upload" class="myFileUploadAction">
	<result name="success">/index.jsp</result>
	<result name="input">/saymessage.html</result>
	<!--struts2默认只能上传2M文件,类型不限。 配置拦截器来限制上传文件的类型和大小 -->
	<interceptor-ref name="fileUpload">
		<!-- 限定文件上传类型 -->
		<param name="allowedTypes">image/png,image/x-png,image/jpg,image/pjpeg,image/bmp,image/gif</param>
		<!-- maximumSize指每个上传文件的大小上限,单位为:byte,这里设为30M,该值不能大于struts.properties中的struts.multipart.maxSize总大小-->
		<param name="maximumSize">31457280</param>
		<!-- 文件上传路径  -->
		<param name="savePath">E:\\UploadFileDemo</param>
				 
	</interceptor-ref>
	<!-- 默认拦截器 -->
	<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

<interceptor-ref name="fileUpload">中name="fileUpload"的名字是固定的。


刚开始我配置好上面的xml后就运行,发现配置的文件上传拦截器文件上传大小没有生效。后来在网上找资料,说要修改struts的上传大小设置,其默认配置是2M,方法有两种:

1、在struts.xml的中添加<constant name="struts.multipart.maxSize" value="209715200" />常量设置

2、在struts.xml同目录下,创建一个struts.properties文件,内容如下:

#文件上传保存路径
struts.multipart.saveDir=E\:\\\\UploadFileDemo
#文件上传大小上限(单位:byte)这里是200M
struts.multipart.maxSize=209715200

#更改上传文件类型不允许的提示信息  
struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8
#更改上传文件太大的提示信息  
struts.messages.error.file.too.large=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u5927\u5C0F\u8D85\u8FC7\u4E0A\u9650\uFF0830M\uFF09
#文件上传其它错误信息  
struts.messages.error.uploading=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u4E0A\u4F20\u7A0B\u5E8F\u53D1\u751F\u5185\u90E8\u9519\u8BEF

我偿试使用第一种方法,结果运行没有效果(不知道是否是什么地方设置不对?)

使用第二种方法就OK了。

另外,在配置完struts.properties后,运行发现提示错误,说struts.multipart.saveDir 为空,所以我就加了这个配置。



<param name="allowedTypes">image/png,image/x-png,image/jpg,image/pjpeg,image/bmp,image/gif</param>

对于文件上传类型限制补充2点:

1、在使用image/x-png,上传png文件时,提示文件类型不允许,后来加入image/png就好了。

2、对于上传jpeg文件,需要image/pjpeg。








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值