javaEE Struts2,文件上传,Action中接收文件类型参数

 

CustomerAction.java(Action对象,接收文件类型参数):

package cn.xxx.web.action;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.xxx.domain.Customer;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	private Customer customer = new Customer();  //Action接收参数的方式,模型驱动和属性驱动等方式可以混用(一起用) 
	
	//上传的文件会自动封装到File对象
	//"photo"要与前台<input type="file" name="photo" />组件 name属性值相同
	private File photo;
	//上传文件的名称会自动封装到xxxFileName属性中
	private String photoFileName;
	//上传文件的MIME类型会自动封装到xxxContentType属性中 
	private String photoContentType;
	

	@Override
	public String execute() throws Exception {
		if(photo!=null){
			System.out.println("文件名称:"+photoFileName);   //abc.jpg
			System.out.println("文件MIME类型:"+photoContentType);  //image/jpeg
			//将上传的文件保存到指定位置
			photo.renameTo(new File("E:/upload/xxx.jpg"));
		}
		// ...................
		
		return "success";
	}

	@Override
	public Customer getModel() {
		return customer;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
	
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值