`
org_zhanghua
  • 浏览: 2124 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Struts2文件拦截器

 
阅读更多

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) + "/";

}

}

}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics