java EE开发之Struts2第四章:国际化和文件上传下载

  struts2中对国际化进行了封装,我们只需要根据其提供的API进行访问就可以。
国际化资源文件的命名格式如下:

baseName_language_country.properties
baseName_language.properties
baseName.properties

其中baseName是资源文件的基本名,我们可以自定义,但language和country必须是java支持的语言和国家。如:
中国大陆: baseName_zh_CN.properties
美国: baseName_en_US.properties

一,定义国际化资源文件

定义国际化资源文件的方式有两种
【1,全局方式】

情况一:对于properties资源文件在src下,只需要通过一个常量来声明.
struts.custom.i18n.resources=testmessages,testmessages2

表示国际化资源文件在src下(或者其他编译路径下),baseName为testmessages,或者testmessage2的资源文件。

情况二:对于properties配置文件可以放置在任意位置
<constant name="struts.custom.i18n.resources" value="com.kz.i18n.resource.message">

代表message.properties在com.kz.i18n.resource包下.

【2,局部方式】

1.针对于action类
位置:与action类在同一个包下.
名称:ActionClassName.properties.
NOTE:这个配置文件只对当前action有效
2.针对于package下所有action
位置:在指定的包下
名称:package.properties
3.jsp页面临时使用某一个properties文件
.<s:i18n name="com.kz.action.package"></s:i18n>

二,在struts2中怎么样操作国际化
struts2中的国际化资源文件可以在3个地方使用
1.在action类中使用
前提:action类要继承ActionSupport类。
getText(String name)就可以获取配置文件中对应名称的值。

2.在validation.xml文件中使用
<message key="名称"/>
NOTE:校验文件中使用,在input页面中input.jsp中通过<s:fielderror/>
获取错误信息!!

3.在jsp页面上使用
<s:text name="名称"> 如果没有使用<s:i18n name="">来指定,会从全局配置文件中
获取。如果要从某一个配置文件中获取,通过name属性来指定, 包名.配置文件名称 .

补充:在struts2国际化配置文件中怎么样使用动态文本
1.action中怎样使用

在资源文件中定义:msg=hello world {0}

在action中可以通过下面的方式获取动态文本

this.getText("msg",new String[]{"tom"})

结果就是 hello world tom

2.jsp页面上怎样使用

在资源文件中定义: msg=hello world {0}

在jsp页面中通过下面的方式获取资源信息

<s:i18n name="com.kz.action.I18nDemo1Action">`
    <s:text name="msg">`
    <s:param>张三</s:param>`
    </s:text>`
</s:i18n>`

结果就是 hello world 张三.

三,文件上传

1,客户端浏览器中药注意3点:

1.method=post
2.<input type="file" name="xx">
3.encType="multipart/form-data";

2,服务端使用struts2进行处理文件上传:
  struts2中使用的使用commons-fileupload组件来完成文件的上传,当然struts2还使用了一个名字为fileupload的拦截器帮助我们进行文件上传操作。

<interceptor name="fileUpload"                      class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

3,struts2中文件上传的具体步骤
第一步:在action中要有三个属性:@kate名字与标签name值一致!

private File upload;
private String uploadContentType;
private String uploadFileName;

文件域中的name为upload,既<input type="file" name="upload">

第二步:execute方法中使用commons-io包下的FileUtils完成文件复制.

FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));

NOTE:当然这是最简单粗暴的方式,实际开发,还要考虑上传文件的名字是否要随机生成等等,留给各位看官自行实现。

4,控制文件文件的大小
  在default.properties文件中定义了文件上传大小

struts.multipart.maxSize=2097152

上传文件默认的总大小 2m如果要修改默认的大小,需要在struts.xml文件中加入如下常量:

 <constant name="struts.multipart.maxSize" value="20971520"></constant>

或者在struts2.properties中加入

 struts.multipart.maxSize=20971520

5,修改struts2中文件上传组件
  在struts2中默认使用的是commons-fileupload进行文件上传。

# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta

如果使用pell,cos进行文件上传,必须导入其jar包.

6,如果上传出现问题,需要配置input视图
在页面上可以通过<s:actionerror><s:fieldsError/>展示错误,可以通过<s:debug></s:debug>来查看错误信息。默认在页面上展示的错误信息,全是英文,要想展示中文,国际化
struts-messages.properties 预定义的错误信息

struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}

通过自定义国际化文件修改为:

struts.messages.error.uploading=上传错误: {0}
struts.messages.error.file.too.large=上传文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3}
struts.messages.upload.error.SizeLimitExceededException=允许上传的最大文件是: {0} 但是你上传的文件大小是: {1}
struts.messages.upload.error.IOException=上传出错!!!: {0}

{0}:中name属性的值
{1}:上传文件的真实名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)

7,多文件上传时的每个上传文件大小控制以及上传文件类型控制.
  只需要将action属性声明成List集合或数组就可以。

private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;

怎样控制每一个上传文件的大小以及上传文件的类型?在fileupload拦截器中,通过其属性进行控制.

maximumSize—每一个上传文件大小
allowedTypes–允许上传文件的mimeType类型.
allowedExtensions–允许上传文件的后缀名.

在struts.xml配置文件中加上参数就可以了

<interceptor-ref name="defaultStack">
    <param name="fileUpload.maximumSize">20971520</param>
    <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>

四,文件下载
  struts2中文件下载是通过<result type="stream">完成。查看struts-default.xml文件中result的定义查看如下类:

<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

在StreamResult类中有三个属性:

用于设置下载文件的mimeType类型
protected String contentType = "text/plain"; 
用于设置进行下载操作以及下载文件的名称
protected String contentDisposition = "inline";
用于读取要下载的文件。
protected InputStream inputStream;

在action类中定义一个方法

<result type="stream">
        <param name="contentType">${contentType}</param>
        <param name="contentDisposition">attachment;filename=${downFilename}</param>
        <param name="inputStream">${inputStream}</param> 
    </result>       

${inputStream}会调用当前action中的getInputStream方法。
${contentType}会调用当前action中的getContentType()方法。
${downFilename}会调用当前action中的getDownFilename()方法。
name要求在Action中提供上面提到的三个方法。

/*获取下载的文件流对象*/
public InputStream getInputStream() throws Exception{
    //手动解决编码
    filename = new String(filename.getBytes("iso-8859-1"),"utf-8");
    FileInputStream fis = new FileInputStream("D:/upload/"+filename);
    return fis;
}
/*获取文件内容的mimeType类型 :*/
public String getContentType(){
    //Servlet api中可以通过ServletContext类的getMimeType();
    String contentType = ServletActionContext.getServletContext().getMimeType(filename);
    return contentType;
}
/*获取文件的名字:兼容不同的浏览器? 编码处理*/
public String getdownFilename() throws Exception{
    return DownloadUtils.getDownloadFileName(ServletActionContext
            .getRequest().getHeader("user-agent"), filename);
}           

public static String getDownloadFileName(String agent, String filename) throws UnsupportedEncodingException {
    if (agent.contains("MSIE")) {
        // IE浏览
        filename = URLEncoder.encode(filename, "utf-8");
    } else if (agent.contains("Firefox")) {
        // 火狐浏览
        BASE64Encoder base64Encoder = new BASE64Encoder();
        filename = "=?utf-8?B?"
                    + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
    } else {
        // 其它浏览
        filename = URLEncoder.encode(filename, "utf-8");
    }
    System.out.println("downloadfilename====="+filename);
    return filename;        
}

NOTE:在struts2中进行下载时,如果使用它有缺陷,例如:下载点击后,取消下载,服务器端会产生异常。在开发中,解决方案:可以下载一个struts2下载操作的插件,它解决了stream问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值