`
shuaigg.babysky
  • 浏览: 571443 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Struts2中的文件上传

阅读更多
package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8926090411591986144L;

	private String allowTypes ;
	
	private int maximumSize ;

	private File upload;
	
	private String uploadFileName;
	
	private String uploadContentType;
	
	private String 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 getSavePath() {
		return savePath;
	}

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

	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}
	
	public int getMaximumSize() {
		return maximumSize;
	}

	public void setMaximumSize(int maximumSize) {
		this.maximumSize = maximumSize;
	}

	@Override
	public String execute() throws Exception {
		String checkUploadType = checkUploadType();
		if(checkUploadType != null) {
			this.addFieldError("upload", "上传文件类型只能是:" + allowTypes);
			return checkUploadType;
		} 
		String checkSize = checkUploadSize();
		if(checkSize != null) {
			this.addFieldError("upload", "上传文件大小大于" + maximumSize);
			return checkSize;
		}
		String saveDir = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(savePath);
		OutputStream out = new FileOutputStream(saveDir + "\\" + uploadFileName);
		InputStream in = new FileInputStream(upload);
		IOUtils.copy(in, out);
		return SUCCESS;
	}
	
	public String checkUploadSize() {
		long uploadLength = upload.length();
		if(uploadLength > maximumSize) {
			return INPUT;
		} else {
			return null;
		}
	}
	
	public String checkUploadType() {
		String result = INPUT;
		String[] types = allowTypes.split(",");
		for (String str : types) {
			if(str.equals(uploadContentType)) {
				return null;
			}
		}
		return result;
	}
}
 
<action name="uploadAction" class="com.test.UploadAction">
			<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
			<param name="maximumSize">10000</param>
			<param name="savePath">/upload</param>
			<result name="input">/upload.jsp</result>
			<result>/success.jsp</result>
		</action>

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
	<s:form action="uploadAction.action" method="post" enctype="multipart/form-data">
		<s:file name="upload" label="选择文件"></s:file>
		<s:submit value="上传"></s:submit>
	</s:form>
</body>
</html>
 

  上面是手动拦截的上传文件类型和上传文件大小,没有使用拦截器,下面写个例子是使用的struts2里面默认配置的拦截器fileUpload实现上传的拦截。

代码如下:需要将struts.xml进行配置

<action name="uploadAction" class="com.test.UploadAction">
			<param name="savePath">/upload</param>
			<interceptor-ref name="fileUpload">
				<param name="maximumSize">10000</param>
				<param name="allowedTypes">image/pjpeg,image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<result>/success.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>

 其中这个allowedTypes如果不这么些,可能会出错,挺恶心的,

使用了这个拦截器后,就能限制文件上传类型和文件上传大小,但是相应的提示信息是英文的,如果想显示的是中文的出错提示,需要对两个地方进行修改。

就是在messageResource.properties文件中,修改这两个东西的值

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

struts.messages.error.file.too.large

 

其中第一个是文件类型,第二个是上传的文件大小。

如果不是这两个错误,而是发生了其他错误,就修改这个值

struts.message.error.uploading

 

做文件上传的时候最好指定这两个属性的值:

struts.multipart.saveDir的值这个是上传临时文件夹,如果不设置,使用的是javax.servlet.context.tempdir的值。也就是work/Catalina/localhost

 

struts.multipart.maxSize是上传的表单的请求内容的最大字节

 

分享到:
评论

相关推荐

    struts2中文件上传过滤codeFilter

    以下是对Struts2中文件上传及`codeFilter`的详细解释: **1. Struts2文件上传机制** Struts2提供了内置的支持来处理文件上传,主要利用了Apache Commons FileUpload库。在Struts2的Action类中,可以定义一个字段,...

    jspstruts1_2struts2 中文件上传

    jspstruts1_2struts2 中文件上传 java文件上传

    struts2中的文件上传和下载示例

    在Struts2中处理文件上传和下载是常见的需求,对于构建交互式的Web应用来说至关重要。以下将详细介绍Struts2中如何实现这两个功能。 一、文件上传 1. 配置依赖:首先,你需要在项目中添加Apache Commons ...

    struts2实现文件上传下载

    首先,我们需要了解Struts2中的文件上传机制。Struts2提供了`FileUploadInterceptor`拦截器来处理文件上传请求。在处理文件上传时,开发者需要在Action类中声明一个`List&lt;FileInfo&gt;`类型的字段,用于接收上传的文件...

    struts2文件上传与下载

    在Struts2中,文件上传和下载是常见的功能需求,主要用于处理用户在Web表单中提交的文件,如图片、文档等。下面将详细介绍Struts2中文件上传和下载的实现方法。 ### 1. 文件上传 #### 1.1 配置Struts2 首先,我们...

    Struts2多个文件上传

    在Struts2中,文件上传功能是一个常用特性,尤其在处理用户提交的多个文件时。本文将详细讲解如何使用Struts2进行多个文件的上传,重点是使用List集合进行上传。 首先,要实现Struts2的文件上传,必须引入必要的...

    struts2 文件的上传和下载

    在Struts2中,文件上传通常涉及到以下几个步骤: 1. **创建上传表单**:在HTML或JSP页面中,使用`&lt;input type="file"&gt;`标签创建一个文件选择框,用户可以通过这个框选择要上传的文件。 2. **配置Action**:在...

    Struts2实现单个文件多个文件上传与下载-多个拦截器

    在Struts2中,文件上传主要依赖于`struts2-convention-plugin`和`struts2-file-uploading-plugin`这两个插件。要实现文件上传,你需要在Action类中定义一个字段,类型为`java.io.File`或`org.apache.struts2....

    struts2文件上传下载源代码

    在Struts2中,文件上传和下载是常见的功能需求,特别是在处理用户交互和数据交换时。这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件...

    struts2实现文件上传

    在 Struts2 中,可以通过配置来控制文件上传的行为,例如最大文件大小、是否启用文件上传等功能。这些配置可以在 `struts.xml` 文件中进行。 ```xml &lt;constant name="struts.multipart.maxSize" value="10485760"/&gt;...

    struts2的上传,下载,删除文件

    在本篇中,我们将聚焦于Struts2中的文件上传、下载和删除功能,这些是Web应用中常见的需求。 1. 文件上传: 在Struts2中,文件上传主要依赖于`Commons FileUpload`库,它处理了多部分表单数据。首先,你需要在`...

    Struts2文件的上传和下载

    在Struts2中,这个过程通过拦截器完成,特别是`fileUpload`拦截器,它负责处理文件上传的细节,并将文件绑定到Action的属性上。 以下是一个简单的Struts2文件上传示例: 1. **前端表单**: 创建一个HTML表单,...

    swfuplaod+struts2实现多文件上传

    3. **创建Struts2 Action**:在Struts2框架中,创建一个处理文件上传的Action类,该类通常会包含一个`List&lt;HttpServletFileWrapper&gt;`类型的属性,用于接收上传的文件。 4. **编写Struts2配置**:在struts.xml配置...

    struts2 实现文件批量上传

    1. **文件上传组件**:在Struts2中,我们通常使用`Commons FileUpload`库来处理文件上传。这个库提供了处理多部分HTTP请求的能力,是Java中处理文件上传的标准库。我们需要在Struts2配置文件中引入对应的拦截器`...

    struts2文件上传

    在本篇文章中,我们将深入探讨Struts2中文件上传的工作原理、实现方法以及相关注意事项。 首先,我们来看一下Struts2文件上传的基本流程: 1. 用户通过HTML表单选择本地文件,并提交到服务器。 2. Struts2拦截器...

    在Struts 2中实现文件上传

    此外,使用 `&lt;s:file&gt;` 标签将文件上传控件与 Action 中的某个字段(如 `myFile`)绑定,这样 Struts 2 就知道如何处理文件上传请求。 下面是一个简单的 `FileUpload.jsp` 示例: ```jsp ; charset=utf-8" ...

    Struts2 文件上传总结

    首先,我们需要了解Struts2中文件上传的基础知识。Struts2通过`struts2-core`库提供的`FileUpload`拦截器来处理文件上传请求。在使用Struts2进行文件上传时,我们需要在Action类中声明一个`java.io.File`类型的字段...

    struts2真正实现上传下载完整源代码

    在Struts2中,实现文件上传和下载功能是一项常见的需求。本文将深入探讨如何使用Struts2来实现这一功能,并结合提供的"struts2真正实现上传下载完整源代码"进行分析。 首先,我们要了解Struts2中文件上传的基本原理...

    Struts2实现文件上传

    在Struts2中,文件上传主要依赖于`org.apache.struts2.components.FileUpload`组件。要实现文件上传,首先需要在Action类中定义一个或多个`File`和对应的`String`类型的属性,`File`属性用于接收上传的文件,而`...

Global site tag (gtag.js) - Google Analytics