Struts2 文件上传

一:表单准备

要想使用 HTML 表单上传一个或多个 文件
    – HTML 表单的 enctype 属性设置为 multipart/form-data
    – 须把 HTML 表单的 method 属性设置为 post
    – 需添加 <input type=“file”> 字段 .
二:Struts2 文件上传原理以及步骤:

①  Struts 应用程序里 , FileUpload 拦截器 Jakarta Commons FileUpload 组件可以完成文件的上传 .
② 步骤 :
     – 1. Jsp 页面的文件上传表单里使用 file 标签 . 如果需要一次上传多个文件 , 就必须使用多个 file 标签 , 但它们的名字必须是相同的
     – 2. Action 中新添加 3 个和文件上传相关的属性 . 3 个属性的名字必须是以下 格式
          [FileName] : File - 被上传的文件。例如: data
          [FileName] ContentType : String - 上传文件的文件类型。例如: dataContentType
          [FileName] FileName : String - 上传文件的文件名。例如: dataFileName
          如果 上上传多个文件 , 可以 使用 List
③ 简单文件上传示例:

表单页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="upload" enctype="multipart/form-data" method="post">
    请选择文件:<input type="file" name="uploadFile">
          <input type="submit" value="上传"/>
 </form>
</body>
</html>
后台上传处理Action:

package com.elgin.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File uploadFile;
	
	//注意如下2个成员变量的命名形式
	private String uploadFileFileName;
	private String uploadFileContentType;

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String execute() {
		ServletContext servletContext = ServletActionContext.getServletContext();
		String desdir = servletContext.getRealPath("/UploadFiles/" + uploadFileFileName);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(uploadFile);
			fos = new FileOutputStream(desdir);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
}
struts.xml 配置文件:

<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
          "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="com" extends="struts-default">
        <action name="upload" class="com.elgin.action.UploadAction">
           <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>
④  如果在文件上传时需要增加相关限制条件,如文件大小、类型以及扩展名 ,可以通过给 FileUpload 拦截器配置参数来实现

      FileUpload 拦截器有 3 个属性可以设置 .
        – maximumSize : 传单个文件 的最大长度 ( 以字节为单位 ), 默认值为 2 MB
       – allowedTypes : 允许上传文件的类型 , 各类型之间以逗号分隔
       – allowedExtensions : 允许上传文件扩展名 , 各扩展名之间以逗号分隔
       – 可以在 struts.xml 文件中覆盖这 3 个属性
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
          "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    
    <package name="com" extends="struts-default">
    
        <interceptors >
          <interceptor-stack name="fileLimit">
	         <interceptor-ref name="defaultStack">
	               <!-- 允许上传的最大文件大小 -->
	               <param name="fileUpload.maximumSize">1048576</param>
	               <!-- 允许上传的文件类型 -->
	               <param name="fileUpload.allowedTypes">text/html,application/msword</param>
	               <!-- 允许上传的文件扩展名 -->
	               <param name="fileUpload.allowedExtensions">html,xml,doc</param>
	           </interceptor-ref>
          </interceptor-stack>
        </interceptors>
        
        <default-interceptor-ref name="fileLimit"></default-interceptor-ref>
        
        <action name="upload" class="com.elgin.action.UploadAction">
           <result name="success">/success.jsp</result>
           <result name="input">/uploadFile.jsp</result>
        </action>
        
    </package>
</struts>
并且可以在国际化资源文件中定制错误消息:

struts.messages.error.uploading - 文件上传出错的消息
struts.messages.error.file.too.large - 文件超过最大值的消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法的消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法的消息
问题: 此种方式定制的消息并不完善. 可以参考 org.apache.struts2 下的 struts-messages.properties, 可以提供更多的定制信息.

注意: 在 org.apache.struts2 下的 default.properties 中有对上传的文件总的大小的限制. 可以使用常量的方式来修改该限制
struts.multipart.maxSize=2097152

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值