struts文件上传下载_国际化

1.文件上传

(1)FileAction类

@ParentPackage("javastruts")

@Namespace("file")

public class FileAction extends CommonAction

{

private static final long serialVersionUID = 1L;

 

@Actions({

@Action(value="/upload",results={

@Result(name="success",location="/main.jsp"),

@Result(name="input",location="/file.jsp")

},

interceptorRefs={

@InterceptorRef(value="fileUpload",params={"allowedExtensions",".jpg,.rar","maximumSize","10485760"}),

@InterceptorRef(value="defaultStack")

}

)

})

public String upload()

{

File saveFile = new File("D:\\upload\\"+uploadFileName);

try {

FileUtils.copyFile(upload, saveFile);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "success";

}

private File upload;

private String uploadFileName;//规定获取文件名的属性

private String desc;

public File getUpload() {

return upload;

}

 

public void setUpload(File upload) {

this.upload = upload;

}

 

public String getDesc() {

return desc;

}

 

public void setDesc(String desc) {

this.desc = desc;

}

 

public String getUploadFileName() {

return uploadFileName;

}

 

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

 

}

 

(2)访问入口file.jsp

<body>

<s:fielderror></s:fielderror><!-- 错误提示 -->

<form action="file/upload" method="post" enctype="multipart/form-data">

<s:text name="abcd"></s:text>:

<input type="file" name="upload"><br />

描述:<input type="text" name="desc"><br />

<input type="submit" value="提交"/>

</form>

</body>

(3)成功跳转main.jsp

<body>

文件上传成功

</body>

(4)文件大小

默认为2M可通过修改struts.xml

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

 

2.文件下载

@Actions({

@Action(value="/download",results={

@Result(name="success",type="stream",

params= {"inputName","downFile",

//与getDownFile对应返回文件流

"contentDisposition","attachment;filename=${oldFileName}"}),

//客户端浏览器处理 附件 弹出对话框下载

}

)

})

public String download()

{

//接收输入的参数ID

//根据ID到数据库查询文件

//处理业务规则

//处理完成

newFileName = "D:\\upload\\111.txt";

try {

oldFileName = new String("运行.txt".getBytes(),"ISO-8859-1");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "success";

}

public InputStream getDownFile(){

FileInputStream fileInputStream =null;

try {

fileInputStream = new FileInputStream(newFileName);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return fileInputStream;

}

private String newFileName;

private String oldFileName;

public String getOldFileName() {

return oldFileName;

}

 

3.国际化

修改文件上传失败的提示信息

(1)创建同名的配置文件FileAction.properties

在struts-default.xml找到相应的提示出错的参数

例如:上传文件的格式错误

struts.messages.error.file.extension.not.allowed=File extension not allowed

(2)中文提示,创建同名的配置文件FileAction_zh_CN.properties

struts.messages.error.file.extension.not.allowed=

\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u7B26

//IOS-8859-1的编码

(3)全局配置国际化

在sturts.xml配置

<constant name="struts.custom.i18n.resources" value="i18n"></constant>

创建名称为i18n.properties的配置文件

struts.messages.error.file.extension.not.allowed=File extension not allowed

(4)全局配置中文(同上)

创建名称为i18n_zh_CN.properties的配置文件

struts.messages.error.file.extension.not.allowed=

\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u7B26

//IOS-8859-1的编码

猜你喜欢

转载自blog.csdn.net/weixin_42756687/article/details/81303308