struts2实现文件上传功能

使用struts2实现文件上传功能,自带的commons-fileupload-1.3.1.jar可以轻松实现文件的上传功能,但是有个问题,如果文件大小远大于初始设置的文件大小值之后,上传就会出现如下图的问题,不知道哪位会处理这种异常,我搞了好几天,就是做不到啊。。。


第一步导包:


第二步,编写静态页面,添加文件上传项

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="uploadFile" method="post" <strong>enctype="multipart/form-data"</strong>>
		<input type="file" name="some"/>
		<input type="submit" value="ok,upload"/>
	</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>
	<constant name="struts.multipart.saveDir" value="/temp/"/>  
	<package name="struts2" namespace="/demo" extends="struts-default">
		<default-action-ref name="pageNotFound"></default-action-ref>
		<global-results><!-- 定义全局Result -->
			<result name="error">/error.jsp</result>
		</global-results>
		<!-- pageNotFound设置  --->
		<action name="pageNotFound">
			<result>/upload.html</result>
		</action>
		<action name="uploadFile" class="com.alex.action.UploadFileAction">
		<exception-mapping result="nullPoint" exception="java.lang.NullPointerException"/>
		<exception-mapping result="ioException" exception="java.io.IOException"></exception-mapping>
		<exception-mapping result="illegal" exception="java.lang.IllegalStateException"></exception-mapping>
			<result name="ok">/success.jsp</result>
			<result name="input">/error.jsp</result>
			<result name="ioException">/error.jsp</result>
			<result name="nullPoint">/error.jsp</result>
			<result name="illegal">/error.jsp</result>
		</action>
		
	</package>
</struts>
第四步,编写action

package com.alex.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.alex.util.FileUtil;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	//拦截器传入的零时文件名称
	private File some;
	private String someFileName;
	
	public File getSome() {
		return some;
	}
	public void setSome(File some) {
		this.some = some;
	}
	public String getSomeFileName() {
		return someFileName;
	}
	public void setSomeFileName(String someFileName) {
		this.someFileName = someFileName;
	}

	public String execute(){
		System.out.println("上传开始,文件名:"+someFileName);
		if(some==null){
			return "error";
		}
		//声明一个路径
		//可以通过文件配置保存路径
		//也可以保存到项目路径下
		
		String path="/WEB-INF/upload/"+someFileName;
		path=ServletActionContext.getServletContext().getRealPath(path);
		System.out.println(path);
		try {
			FileUtil.copy(some, path);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return "ok";
	}

}
第五步,编写FileUtil类

package com.alex.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {
	public static boolean copy(File src,String dest) throws IOException{
		if(src==null||dest==null){
			throw new RuntimeException("参数异常");
		}
		
		FileInputStream fis=new FileInputStream(src);
		BufferedInputStream bis=new BufferedInputStream(fis);
		
		FileOutputStream fos=new FileOutputStream(dest);
		BufferedOutputStream bos=new BufferedOutputStream(fos);
		
		byte[] buf=new byte[1024];
		int temp=0;
		while((temp=bis.read(buf))!=-1){
			bos.write(buf, 0, temp);
		}
		bis.close();
		bos.close();
		return true;
	}
}

最后随便写个success.jsp和error.jsp就OK了,上传问题不大,就是有一个问题,上传比设定文件大小稍微大一些的文件时,可以正确的跳到input设置去,可以文件超级大了,测试时,就直接悲剧了,我表示我所知道的办法都试过了,浪费了好几天,就是无法捕获这个exception啊,各位有什么办法啊



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值