Ubuntu开发Struts2应用(4、文件上传)

文件上传这个功能在项目应用是非常常见的,这节说说Struts2如何实现文件上传。

Struts2使用开源项目Apache Jakarta Commons FileUpload和内建的FileUploadInterceptor拦截器实现文件上传

下面介绍代码:

FileUploadAction

  

package com.linuxsight.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class FileUploadAction {

 private File upload;
 private String uploadFileName;
 private String uploadContentType;
 private String allowTypes;
 private String savePath;

 public String getSavePath() {
  return savePath;
 }

 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }

 public File getUpload() {
  return upload;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public String getUploadFileName() {
  return uploadFileName;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getUploadContentType() {
  return uploadContentType;
 }

 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 public String getAllowTypes() {
  return allowTypes;
 }

 public void setAllowTypes(String allowTypes) {
  this.allowTypes = allowTypes;
 }

 public String execute() throws Exception{

  String realpath = ServletActionContext.getServletContext().getRealPath(this.getSavePath());
  System.out.println("realpath---"+realpath);
  ActionContext.getContext().put("realpath",realpath);
  System.out.println("uploadFileName-----"+uploadFileName);

//判断是否允许上传
  String filterResult =filterType(this.getAllowTypes().split(","));
  System.out.println("filterResult:========="+filterResult);
  if(filterResult != null){
   ActionContext.getContext().put("error","上传的文件类型不正确");
   return filterResult;  
  }

  if(upload!=null){
   File savefile = new File(new File(realpath), uploadFileName);
   if(!savefile.getParentFile().exists()) {
    savefile.getParentFile().mkdirs();
   }
   FileUtils.copyFile(upload, savefile);

  }
  return "success";
 }

 public String filterType(String[] types){
  String fileType= this.getUploadContentType();
  for(String type:types){
   if(type.equals(fileType)){
    return null;
   }
  }
  return "input";
  }
  }

struts.properties(控制上传文件大小)

struts.multipart.maxSize=2097152

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
     <!-- 文件上传 -->
     <package name="fileUpload"  extends="struts-default">
  <action name="fileUpload">
  <param name="allowTypes">
        image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg
        </param>
        <param name="savePath">/upload</param>
   <result>/WEB-INF/jsp/suc.jsp</result>
   <result name="input">/upload.jsp</result>
  </action>
 </package>
</struts>

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>文件上传</title>
  </head>
  <h3>struts2文件上传</h3>
 <body>
 ${error }
    <form action="${pageContext.request.contextPath}/fileUpload" enctype="multipart/form-data" method="post">
     文件:<input type="file" name="upload">
     <input type="submit" value="上传"/>
    </form>
  </body>
</html>

suc.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>上传成功页面</title>
  </head>
  <h3>Struts2文件上传|Linux视野</h3>
  <body>
    恭喜,文件上传成功 <br>
   <b> 文件在ubuntu环境下的路径:</b><br>
   (其实在这里久可以看到ubuntu下tomcat的部署目录,该文件夹是隐藏的)
   <br>
    ${realpath }<br>
 <b>文件名:</b>${uploadFileName}<br>
 <b>文件类型:</b>${uploadContentType}<br>
     <!--  <img src="<s:property value="'upload/' + uploadFileName"/>" />
     <img alt="" src="${pageContext.request.contextPath}/upload/${uploadFileName }" />
     -->
     <img alt="${uploadFileName}" src="upload/${uploadFileName }" />
  
  </body>
</html>
注意:在文件上传过程中出现了一个警告

警告: Could not find action or result
There is no Action mapped for action name images/. - [unknown location]

所以我们在web.xml多加上一个ActionContextCleanUp的配置,就不会有警告了。这个类是一个filter,他的作用是方便strut2与sitemesh整合,与文件上传应没有关系,但不加却会出现莫名的警告。

<filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ActionContextCleanUp
  </filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts-cleanup</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping><filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ActionContextCleanUp
  </filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts-cleanup</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

实现效果图:

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: