Struts2使用common-fileupload组件上传,在项目工程中需要添加common-fileupload.jar和common-io.jar 2个jar
struts.xml配置文件拦截器如下:
<package name="basePackage" extends="struts-default">
<interceptors>
<interceptor-stack name="baseStack">
<!-- 默认异常拦截器 -->
<interceptor-ref name="exception" />
<!-- 异常拦截器 -->
<interceptor-ref name="exceptionInterceptor" />
<!-- 字符串拦截器 -->
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="multiselect" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<!-- 配置方法级别的校验 -->
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
Action配置如下:
@ParentPackage("xxx")
public class ServiceOrderAction extends BaseAction {
//文件上传工具类
private FileUpload upload;
public String upload()throws exception{
//设置Fileupload工具类的savePath路径
upload.setSavePath("/" + Constant.UPLOAD_FILE_DIR);// 设置附件上传路径
upload.execute();//执行上传
return SUCCESS;
}
}
文件上传工具类FileUpload配置如下:
public class FileUpload {
public static final String STATUS = "status";
public static final String WARN = "warn";
public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String MESSAGE = "message";
public static final Integer FileUpLoadFail = 0;//文件上传失败
public static final Integer FileUpLoadSuccess = 1;//文件上传成功
public static final Integer FileExist = 2;//文件已存在
public static final Integer FileFormatError = 3;//文件格式错误
private File file; // 待上传文件 //声明一个File 类型的属性 file任意取名
private String fileFileName;
//获取待上传的文件名,以xxxFileName结尾
private String fileContentType;
//获取上传文件的类型,以xxxContentType结尾
private String formSuffix; // 待上传表单文件扩展名
/**
* 文件保存路径(相对路径,根目录为WebRoot)
* 默认为流程定义文件上传路径
*/
private String savePath = "";
private String fileName = ""; // 文件保存名称
private String serverFilePath = ""; //服务器文件目录+刚上传的文件名
/**
* 可上传文件扩展名,多个扩展名用","分隔,"*"表示不限制
* 默认为流程定义文件扩展名
*/
private String suffix = "xls,XLS,xlsx,XLSX,doc,DOC,docx,DOCX,txt,TXT";
private int uploadLimit = 0; // 文件上传最大值,0表示无限制,单位M
/**
* 执行上传文件
* return
* 0 上传失败
* 1 上传成功
* 2 文件已经存在
*/
public int execute() {
try {
File uploadDir = new File(ServletActionContext.getServletContext().getRealPath(savePath));
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
long crrtime= System.currentTimeMillis();
String ym=DateUtil.getFormateDate("yyyyMM"); //年月
String uploadPath = uploadPath = savePath + ym+"/"+crrtime+"."+formSuffix;
uploadDir = new File(ServletActionContext.getServletContext().getRealPath(uploadPath));
if(uploadDir.exists()){
return FileExist;//文件存在
}
FileUtils.copyFile(file, uploadDir); //使用common-io.jar FileUtils.copyFile()方法进行上传
this.serverFilePath = uploadPath;
return FileUpLoadSuccess;
} catch (IOException e) {
e.printStackTrace();
return FileUpLoadFail;
}
/**
* 文件保存路径(相对路径,根目录为WebRoot)
* 默认为流程定义文件上传路径
* 将路径转换成小写字母
*/
public void setSavePath(String savePath) {
setSavePath(savePath,true);
}
/**
* 文件保存路径(相对路径,根目录为WebRoot)
* 默认为流程定义文件上传路径
* b=true转换路径为小写字母,b=false保持原样
*/
public void setSavePath(String savePath,boolean b) {
if (!StringUtil.toString(savePath).equals("")) {
if (savePath.endsWith("/")) {
this.savePath = b?savePath.toLowerCase():savePath;
}else {
this.savePath = (b?savePath.toLowerCase():savePath) + "/";
}
}
}
* 文件保存路径(相对路径,根目录为WebRoot)
* 默认为流程定义文件上传路径
* 将路径转换成小写字母
*/
public void setSavePath(String savePath) {
setSavePath(savePath,true);
}
/**
* 文件保存路径(相对路径,根目录为WebRoot)
* 默认为流程定义文件上传路径
* b=true转换路径为小写字母,b=false保持原样
*/
public void setSavePath(String savePath,boolean b) {
if (!StringUtil.toString(savePath).equals("")) {
if (savePath.endsWith("/")) {
this.savePath = b?savePath.toLowerCase():savePath;
}else {
this.savePath = (b?savePath.toLowerCase():savePath) + "/";
}
}
}
}
分享到:
相关推荐
struts2 单文件,多文件上传及异常处理
struts2上传单个文件及多个文件,strus2拦截器的使用,里面有个user用户,先执行user.jsp即登陆,可以执行上传文件的操作,反之,不能,有个exit.jsp,退出之后不能执行。里面含有jar文件,导入myeclipse可以执行...
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三、定义Struts2拦截器。 ...
该文件主要针对springMVC+spring+mybatis框架配置,struts2文件上传的代码,Struts2自定义拦截器配置
struts2(拦截器_文件上传_输入验证_国际化)
Struts2默认拦截器配置文件,里面包括了Struts2默认的拦截器配置,是很好的参考资料
通过struts2来自定义一个拦截器,需要定义struts2.xml和web.xml文件,详细代码见下载。
模拟Struts2拦截器代码,模拟struts2拦截器的实现
传智播客struts2_3(拦截器_文件上传_输入验证_国际化).ppt
struts2文件上传,配置了拦截器,只能穿txt,小于10M, 取消限制可以去struts.xml修改,注释详细
传智播客3_struts2(拦截器_文件上传_输入验证_国际化).ppt
Struts 2是通过Commons FileUpload文件上传...Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件
Struts2实现单个文件 多个文件 上传与下载 多个拦截器 直接导入MyEclipse包中即可使用。
struts 拦截器 文件上传
在struts2框架中主配置文件struts-defalut.xml 文件中定义了大量的拦截器和拦截器栈。通过 default-interceptor-ref元素定义了 当前应用的默认拦截器栈,对用户的每次请求都需要调用拦截器栈中的每个拦截器来拦截...
自定义的简单的struts,自己写一个和struts一样的xml文件,利用反射进行生成实例和注入参数,自定义拦截器和action
文件的国际化字符串中使用OGNL,格式为${表达式},例如: validation.required=${getText(fileName)} is required 2. 使用java.text.MessageFormat中的字符串格式,格式为{ 参数序号(从0开始), 格式类形(number |...
完整可运行起来的SSH项目!中使用了SAIF 插件 实现拦截器的效果!并且有sql语句 !我使用的是mysql数据库,如果你使用别的数据库请改配置文件!运行成功后注意控制台的输出
其中包含两个项目,都是struts2中的拦截器的用法,还有配置文件。
Struts2文件上传,使用Strut2的拦截器来实现进度条显示,完整代码,包含了所有的jar包,可直接运行