欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2实现文件上传功能实例解析

程序员文章站 2024-03-08 13:49:10
一、 搭建struts2环境 在myeclipse下,右击项目->myeclipse->project facets->install apache s...

一、 搭建struts2环境

在myeclipse下,右击项目->myeclipse->project facets->install apache struts2。

如要自己搭建,需下载struts2包,写struts.xml配置文件。

web.xml文件配置如下:

<filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>*.action</url-pattern>
 </filter-mapping>

二、 文件上传

1.前台页面:

上传页面:

<body>
 <form action="upload.action" method="post" enctype="multipart/form-data">
  <input type="file" name="upload"/>
  <input type="submit" value="提交"/>
  <br>
  ${result}
 </form>
 </body>

input name属性与后台命名一致。

上传失败页面:

<body>
 <h2>上传失败</h2>
 <s:fielderror></s:fielderror>
 </body>

需:

<%@ taglib uri="/struts-tags" prefix="s"%>

2.后台action

主要属性upload,uploadcontenttype,uploadfilename。

package com.yf.action;
import java.io.file;
import org.apache.commons.io.fileutils;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.actionsupport;
public class uploadaction extends actionsupport{
 private file upload;
 private string uploadcontenttype;
 private string uploadfilename;
 private string result;
 public file getuoload() {
  return upload;
 }
 public void setupload(file upload) {
  this.upload = upload;
 }
 public string getuploadcontenttype() {
  return uploadcontenttype;
 }
 public void setuploadcontenttype(string uploadcontenttype) {
  this.uploadcontenttype = uploadcontenttype;
 }
 public string getuploadfilename() {
  return uploadfilename;
 }
 public void setuploadfilename(string uploadfilename) {
  this.uploadfilename = uploadfilename;
 }
 public string getresult() {
  return result;
 }
 public void setresult(string result) {
  this.result = result;
 }
 @override
 public string execute() throws exception {
  string path = servletactioncontext.getservletcontext().getrealpath("/images");
  file file = new file(path);
  if(!file.exists()){
   file.mkdir();
  }
  system.out.println(upload);
  fileutils.copyfile(upload, new file(file,uploadfilename));
  result = "上传成功";
  return success;
 }
}

3.struts.xml文件配置

配置action及配置拦截器限制上传文件的类型及大小。

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.enable.dynamicmethodinvocation" value="false"/>
 <constant name="struts.devmode" value="true"/>
 <constant name="struts.multipart.savedir" value="/tmp"/>
 <constant name="struts.custom.i18n.resources" value="app"></constant>
 <package name="default" namespace="/" extends="struts-default">
  <action name="upload" class="com.yf.action.uploadaction">
  <result>/index.jsp</result>
  <result name="input">/error.jsp</result>
  <!-- 配置拦截器限制上传文件的类型及大小 -->
  <interceptor-ref name="fileupload">
   <param name="allowedtypes">image/bmp,image/x-png,image/gif,image/jpeg</param>
   <param name="maximumsize">2m</param>
  </interceptor-ref>
  <interceptor-ref name="defaultstack"></interceptor-ref>
  </action>
 </package>
</struts> 

4.新建properties文件

文件上传失败信息显示到前台,处理显示出错信息。

Struts2实现文件上传功能实例解析

文件内容如下:

struts.messages.error.file.too/large=\u4e0a\u4f20\u6587\u4ef6\u592a\u5927\u4e86\uff01
struts.messages.error.content.type.not.allowed=\u4e0a\u4f20\u6587\u4ef6\u7c7b\u578b\u4e0d\u7b26\uff01

即添加:

name:struts.messages.error.file.too/large

value : 上传文件太大了!

name : struts.messages.error.content.type.not.allowed

value: 上传文件类型不符!

运行结果如下:

选择jpg图片,大小不超过2m,运行后

Struts2实现文件上传功能实例解析

选择非图片文件:

Struts2实现文件上传功能实例解析

如需批量上传文件,将后台upload,uploadcontenttype,uploadfilename均改为list,循环读取上传文件保存到硬盘,前台加入input ,name属性一致。

以上所述是小编给大家介绍的struts2实现文件上传功能实例解析,希望对大家有所帮助