2011级-csdn-java-张侃—struts2文件下传异常信息国际化的处理(案例)

2011级-****-java-张侃—struts2文件上传错误信息国际化的处理(案例)

2011级-****-java-张侃—struts2文件下传异常信息国际化的处理(案例)

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<form
			action="${pageContext.request.contextPath}/****/UploadAction_upload.action"
			method="post" enctype="multipart/form-data">
			上传文件:
			<input type="file" name="upload">
			<br />
			<input type="submit" value="上传" />
		</form>
		错误信息:
		<br />
		<s:fielderror></s:fielderror>
	</body>
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
	    <filter-name>struts2</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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>
   <!-- 常量的配置 -->
   <include file="struts-constant.xml"/>
   
   
   
    <package name="user" namespace="/****" extends="struts-default">
     
        <global-results>
           <result name="input">/index.jsp</result>
        </global-results>
    
       <action name="UploadAction_*" class="www.****.international_upload.action.UploadAction" method="{1}">
            <interceptor-ref name="fileUpload">
                <!-- 设置文件上传的类型:Tomcat/conf/web.xml文件 就有文件的类型的名称 -->
                <param name="allowedTypes">image/jpeg,image/pjpeg,application/octet-stream,application/x-zip-compressed</param>
                <param name="maximumSize">5242880</param>
            </interceptor-ref>
              <!-- 默认的拦截器栈 -->
            <interceptor-ref name="defaultStack"/>
            <result name="success">/index.jsp</result>
       </action>
    </package>

</struts>


struts-constant.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>
   <!-- 常量的配置 -->
   <!-- struts2的后缀 -->
   <constant name="struts.action.extension" value="action"/>
   <!-- 编码方式 -->
   <constant name="struts.i18n.encoding" value="UTF-8"/>
   <!-- 浏览器静态缓存最好处于关闭状态 -->
   <constant name="struts.serve.static.browserCache" value="false"/>
   <!-- struts.xml文件当被修改后 重新加载,开发阶段最好打开 -->
   <constant name="struts.configuration.xml.reload" value="true"/>  
   <!-- 处于开发阶段 最好把开发模式打开 会打印更多的详细错误信息 -->
   <constant name="struts.devMode" value="true"/>
   
   <!-- 设置临时保存的路径 -->
   <constant name="struts.multipart.saveDir" value="c:\\day\\"/>
   <!-- 设置文件上传的大小 ,这个值必须大于等于拦截器中的值 -->
   <constant name="struts.multipart.maxSize" value="5242880"/>
   
     <!-- 国际化资源文件的基本名称 -->
   <constant name="struts.custom.i18n.resources" value="****"/>
   

</struts>


UploadAction.java

package www.****.international_upload.action;

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

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.FileUploadInterceptor;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private File file;

	private String contentType;

	private String filename; 

	

	public void setUpload(File file) {
		this.file = file;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String filename) {
		this.filename = filename;
	}

	public String upload() {
		
		String path = ServletActionContext.getServletContext().getRealPath(
				"WEB-INF/uploads");
		
	
		File pfile = new File(path);
		
		if (!pfile.exists()) {
			pfile.mkdirs();
		}

		try {
			FileUtils.copyFile(file, new File(pfile, System.currentTimeMillis()
					+ "_" + filename));
			return SUCCESS;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return INPUT;
	}

}

两个配置文件:

2011级-****-java-张侃—struts2文件下传异常信息国际化的处理(案例)

 


2011级-****-java-张侃—struts2文件下传异常信息国际化的处理(案例)

 

 

运行结果界面:

2011级-****-java-张侃—struts2文件下传异常信息国际化的处理(案例)