Struts2之文件上传

阅读更多

Struts 2 - 文件上传

参考文章:

http://www.dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-file-upload-example-1.html

 

拦截器(Interceptor):FileUploadInterceptor

通过拦截器的设置,我们可以定义

1.上传文件的类型(allowedTypes)

2.上传文件的扩展名(allowedExtensions)

3.上传文件的最大限制(maximumSize)bytes


  
     10240
  
    
     image/png,image/gif,image/jpeg
  
    
     .png,.gif,.jpeg
  

 

Demo:

1. index.jsp - 创建file upload file form


	
	

 2. FileUploadAction.java - 创建具体的Action class.

需要包括三个属性:

File image

String imageContentType

String imageFileName

image这个名字需要和s:filename属性保持一致。

 

在Action class里实现具体的文件“拷贝”功能,即,在服务器的指定路径生成上传文件。

这里需要注意的是,如果什么都不写,只是简单的生成一个空的Action类,我们可以在eclipse的console里得到类似如下的路径信息:

C:\repository\upload_39d5eb38_6bab_496c_ba9d_8f05261ec0e2_00000000.tmp

这个tmp文件只一个临时文件,里面保存了上传文件的内容,有可能是字符流或者字节流。并且此文件在上传结束后会自动删除。

 

再说明下上传路径的定义。

这篇文章介绍了具体的设置方式

http://mossad.iteye.com/blog/1522905

在本例中,在struts.xml里的设置如下:

下面是struts.xml的全部内容:





	
	
	 
	
	
		
			/Result.jsp
		
	

 

下面是FileUploadAction的全部内容:

package org.apache.struts.helloworld.action;

import java.io.File;

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

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

	private static final long serialVersionUID = 8938269342974350902L;

	private File image;
	private String imageContentType;
	private String imageFileName;
	
	@Override
	public String execute() throws Exception {
		String realpath = ServletActionContext.getServletContext().getRealPath("/repository");
		System.out.println(realpath);
		System.out.println(image.getAbsolutePath());
		File file = new File(realpath);
		if(!file.exists()){
			file.mkdirs();
		}
		FileUtils.copyFile(image, new File(file, imageFileName));
		return SUCCESS;
	}
	
	public File getImage() {
		return image;
	}
	
	public void setImage(File image) {
		this.image = image;
	}
	
	public String getImageContentType() {
		return imageContentType;
	}
	
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	
	public String getImageFileName() {
		return imageFileName;
	}
	
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	
}

 这段代码会在 xxx web工程的根目录下生成一个repository文件夹来存放最后的上传文件。

例如:

..\apache-tomcat-7.0.42\webapps\basic_struts-1.0.0\repository

这里需要注意,临时存放的目录和最终的存放目录为两个不同的路径:

临时目录定义在了struts.xml,本例是:c:\repository\

文件存放路径是通过如下代码实现的:

 

String realpath = ServletActionContext.getServletContext().getRealPath("/repository");

File file = new File(realpath);
if(!file.exists()){
   file.mkdirs();
}
FileUtils.copyFile(image, new File(file, imageFileName));

 

3.Result.jsp - 创建上传成功的结果页面。

最后用一个简单的页面显示出成功上传的文件名和类型:

<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	File Name: ${imageFileName} 
Content Type: ${imageContentType}

 

说明:

这里没有显示的去定义Action的拦截器,因为basicstruts2 继承了struts-default。在default里已经包含了

FileUploadInterceptor。如果需要特别的定义,我们可以在Action里插入

 

 

你可能感兴趣的:(struts2,fileupload)