struts 上传 和错误信息格式化
2014-02-14 15:04
246 查看
单文件上传
1. 通过HTML表单上传文件时, 需要将表单的enctype属性设置为multipart/form-data, method属性设置为post.
jsp页面代码:
Html代码
<form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"
name="form1" method="post" enctype="multipart/form-data">
上传文件名称: <input type="file" name="strutsUpload">
<input type="submit" value="上传">
</form>
2. 在Action类中定义一下3个成员变量, 并为其提供getter和setter方法:
Java代码
private File strutsUpload; // 上传的文件
private String strutsUploadContentType;// 文件的类型
private String strutsUploadFileName;// 文件的名称
以上3个成员变量的名称不能随意更改, private File strutsUpload变量的名称必须和jsp中上传文件标签中的name属性的值一致. 而private String strutsUploadContentType变量的名称必须为"上传文件的名称+ContentType", private String strutsUploadFileName变量的名称必须为"上传文件的名称+FileName".
3. 在Action类中定义业务方法. 完整的Action类可以如下:
Java代码
public class UploadAction extends ActionSupport {
private File strutsUpload; // 上传的文件
private String strutsUploadContentType;// 文件的类型
private String strutsUploadFileName;// 文件的名称
// 业务方法
public String saveFile() {
try {
ServletContext context = ServletActionContext.getServletContext();
// 获得当前web应用所在目录下file文件夹的绝对路径
String path = context.getRealPath("/file");
File destFile = new File(path, strutsUploadFileName);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileUtils.copyFile(strutsUpload, destFile);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return "success";
}
// 省略getter和setter方法
}
4. 在struts.xml文件中配置uploadFile拦截器的属性:
Xml代码
<package name="upload" namespace="/upload" extends="struts-default">
<action name="uploadAction_*" class="cn.xing.upload.UploadAction" method="{1}">
<interceptor-ref name="defaultStack">
<!--
修改允许上传文件的大小(默认值是2M),
将调用FileUploadInterceptor中的setMaximumSize(223434555)
-->
<param name="fileUpload.maximumSize">223434555</param>
<!-- 配置允许上传文件的类型,如果有多个类型用","隔开 -->
<param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>
<!--配置允许上传文件的扩展名,如果有多个用","隔开 -->
<param name="fileUpload.allowedExtensions">txt,excel,ppt</param>
</interceptor-ref>
<result name="success">/upload/success.jsp</result>
<result name="input">/upload/error.jsp</result>
</action>
</package>
多文件上传
多文件上传与单文件上传类似, 只有jsp表单和Action类的代码有所不同.
1. jsp表单代码:
Html代码
<form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action"
name="form1" method="post" enctype="multipart/form-data">
上传文件名称: <input type="file" name="strutsUploads"><br>
上传文件名称: <input type="file" name="strutsUploads"><br>
上传文件名称: <input type="file" name="strutsUploads"><br>
<input type="submit" value="上传">
</form>
注意每个文件上传标签的name属性需要一致.
2. Action类:
Java代码
public class UploadsAction extends ActionSupport {
private File[] strutsUploads;
private String[] strutsUploadsContentType;
private String[] strutsUploadsFileName;
public String saveFiles() {
ServletContext context = ServletActionContext.getServletContext();
String realpath = context.getRealPath("/file");
try {
if (strutsUploads != null && strutsUploads.length > 0) {
for (int i = 0; i < strutsUploads.length; i++) {
File destFile = new File(realpath, strutsUploadsFileName[i]);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileUtils.copyFile(strutsUploads[i], destFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
// 省略getter和setter方法
}
多文件上传时, Action中的3个成员变量的名称需要遵循与单文件上传时相同的规则. 此时3个成员变量均为数组.
3. 在struts.xml文件中配置uploadFile拦截器的属性, 同上.
错误显示
当文件上传过程中出错时, 如果定义了错误显示页面, 将跳转到指定的页面, 并输出错误信息.
1. 在action标签下定义如下子标签:
<!-- 定义上传出错要转向的页面 -->
<result name="input">/upload/error.jsp</result>
2. /upload/error.jsp页面可以使用<s:fielderror/>标签输出错误信息.
前提是已经使用taglib指令导入了struts标签库:
<%@ taglib uri="/struts-tags" prefix="s"%>
3. 默认的错误信息为英文, 在struts2-core-2.x.x.x.jar\org\apache\struts2\struts-messages.properties文件中定义:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File too large: {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}
{0}:<input type=“file” name=“uploadImage”>中name属性的值
{1}:上传文件的名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
我们可以在Action的统计目录下创建一个fileuploadmessage.properties文件, 文件名没有要求, 但必须是properties文件, 在其中输入:
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}
使用jdk目录下的native2ascii工具将中文转为unicode编码.
接下来需要在struts.xml文件中加载自定义的资源文件:
<constant name="struts.custom.i18n.resources" value="cn.xing.upload.fileuploadmessage"></constant>
1. 通过HTML表单上传文件时, 需要将表单的enctype属性设置为multipart/form-data, method属性设置为post.
jsp页面代码:
Html代码
<form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"
name="form1" method="post" enctype="multipart/form-data">
上传文件名称: <input type="file" name="strutsUpload">
<input type="submit" value="上传">
</form>
2. 在Action类中定义一下3个成员变量, 并为其提供getter和setter方法:
Java代码
private File strutsUpload; // 上传的文件
private String strutsUploadContentType;// 文件的类型
private String strutsUploadFileName;// 文件的名称
以上3个成员变量的名称不能随意更改, private File strutsUpload变量的名称必须和jsp中上传文件标签中的name属性的值一致. 而private String strutsUploadContentType变量的名称必须为"上传文件的名称+ContentType", private String strutsUploadFileName变量的名称必须为"上传文件的名称+FileName".
3. 在Action类中定义业务方法. 完整的Action类可以如下:
Java代码
public class UploadAction extends ActionSupport {
private File strutsUpload; // 上传的文件
private String strutsUploadContentType;// 文件的类型
private String strutsUploadFileName;// 文件的名称
// 业务方法
public String saveFile() {
try {
ServletContext context = ServletActionContext.getServletContext();
// 获得当前web应用所在目录下file文件夹的绝对路径
String path = context.getRealPath("/file");
File destFile = new File(path, strutsUploadFileName);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileUtils.copyFile(strutsUpload, destFile);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return "success";
}
// 省略getter和setter方法
}
4. 在struts.xml文件中配置uploadFile拦截器的属性:
Xml代码
<package name="upload" namespace="/upload" extends="struts-default">
<action name="uploadAction_*" class="cn.xing.upload.UploadAction" method="{1}">
<interceptor-ref name="defaultStack">
<!--
修改允许上传文件的大小(默认值是2M),
将调用FileUploadInterceptor中的setMaximumSize(223434555)
-->
<param name="fileUpload.maximumSize">223434555</param>
<!-- 配置允许上传文件的类型,如果有多个类型用","隔开 -->
<param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>
<!--配置允许上传文件的扩展名,如果有多个用","隔开 -->
<param name="fileUpload.allowedExtensions">txt,excel,ppt</param>
</interceptor-ref>
<result name="success">/upload/success.jsp</result>
<result name="input">/upload/error.jsp</result>
</action>
</package>
多文件上传
多文件上传与单文件上传类似, 只有jsp表单和Action类的代码有所不同.
1. jsp表单代码:
Html代码
<form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action"
name="form1" method="post" enctype="multipart/form-data">
上传文件名称: <input type="file" name="strutsUploads"><br>
上传文件名称: <input type="file" name="strutsUploads"><br>
上传文件名称: <input type="file" name="strutsUploads"><br>
<input type="submit" value="上传">
</form>
注意每个文件上传标签的name属性需要一致.
2. Action类:
Java代码
public class UploadsAction extends ActionSupport {
private File[] strutsUploads;
private String[] strutsUploadsContentType;
private String[] strutsUploadsFileName;
public String saveFiles() {
ServletContext context = ServletActionContext.getServletContext();
String realpath = context.getRealPath("/file");
try {
if (strutsUploads != null && strutsUploads.length > 0) {
for (int i = 0; i < strutsUploads.length; i++) {
File destFile = new File(realpath, strutsUploadsFileName[i]);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileUtils.copyFile(strutsUploads[i], destFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
// 省略getter和setter方法
}
多文件上传时, Action中的3个成员变量的名称需要遵循与单文件上传时相同的规则. 此时3个成员变量均为数组.
3. 在struts.xml文件中配置uploadFile拦截器的属性, 同上.
错误显示
当文件上传过程中出错时, 如果定义了错误显示页面, 将跳转到指定的页面, 并输出错误信息.
1. 在action标签下定义如下子标签:
<!-- 定义上传出错要转向的页面 -->
<result name="input">/upload/error.jsp</result>
2. /upload/error.jsp页面可以使用<s:fielderror/>标签输出错误信息.
前提是已经使用taglib指令导入了struts标签库:
<%@ taglib uri="/struts-tags" prefix="s"%>
3. 默认的错误信息为英文, 在struts2-core-2.x.x.x.jar\org\apache\struts2\struts-messages.properties文件中定义:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File too large: {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}
{0}:<input type=“file” name=“uploadImage”>中name属性的值
{1}:上传文件的名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
我们可以在Action的统计目录下创建一个fileuploadmessage.properties文件, 文件名没有要求, 但必须是properties文件, 在其中输入:
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}
使用jdk目录下的native2ascii工具将中文转为unicode编码.
接下来需要在struts.xml文件中加载自定义的资源文件:
<constant name="struts.custom.i18n.resources" value="cn.xing.upload.fileuploadmessage"></constant>
相关文章推荐
- SSH框架Struts文件上传自定义错误提示信息解决方案
- struts 2.3.8中可能导致上传文件时获取不到文件信息的错误原因
- 【ssh】struts文件上传拦截器(单,多文件上传,以及错误信息的返回)
- struts错误信息提示
- PHP同时上传“多个”文件示例,并格式化$_FILES数组信息
- 使用context.Request.Files出现错误: 上传失败,错误信息:SyntaxError: Unexpected number
- jquery 的uploadify上传时出错信息提示成中文!比如文件大小限制提示错误File size:Error
- Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法
- 解决文件无法上传的问题,错误信息:System.IO.DirectoryNotFoundException: Could not find a part of the path.
- Struts中错误(异常)信息的处理
- php 文件上传 错误信息 总结 $_FILES['Filedata']["error"] 值枚举
- 四、struts学习之model接收参数和提示错误信息对象
- struts错误信息
- 帮帮忙了,我是初学者在学习Struts2时关于文件上传时的错误,不知道怎么去改了,让我花费一下午的时间还是不对,一直提示我配置不对
- Struts上传文件报java.io.FileNotFoundException错误
- struts上传文件失败 Content-Type not allowed错误解决方法【转】
- struts 图片上传 错误
- 牛腩购物33:发布网站,将网站上传到外网,讲数据库发布到远程服务器,如何在服务器上开数据库,更换电脑后微软企业库报错,网站异常后发送错误信息到指定邮箱,设置错误页
- struts 上传图片(classnofind一般都是jar文件的错误)
- struts2文件上传与错误信息国际化