Struts2(传智播客笔记4)

单文件上传
文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。
文件下载地址:
http://commons.apache.org下载。
第二步:把form表的enctype设置为"multipart/form-data",如下:
<form enctype="multipart/form-data" action="control/employee/upload.action" method="post">
    文件:<input type="file" name="image"/>
    <input type="submit" value="上传"/>
</form>
第三步:在Action类中添加以下属性
package cn.jbit.struts2.action;

import java.io.File;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionContext;

public class FileUploadAction {

	private Logger logger = Logger.getLogger(FileUploadAction.class);
	private File image;
	private String imageFileName;// 文件名
	private String imageContentType;// 文件类型

	public String execute() throws IOException {
		// images/
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/images");
		logger.debug("realPath->" + realPath);
		logger.debug("imageFileName->" + imageFileName);
		if (null != image) {
			File destFile = new File(new File(realPath), imageFileName);
			logger.debug("destFile.parent->" + destFile.getParent());
			// exists() true->存在 false->不存在
			logger.debug("destFile.parentFile.exists->"
					+ destFile.getParentFile().exists());
			if (!destFile.getParentFile().exists()) {
				// 创建指定目录
				destFile.getParentFile().mkdirs();
			}
			// 上传文件
			FileUtils.copyFile(image, destFile);
			ActionContext.getContext().put("message",
					imageFileName + "上传成功(文件类型:" + imageContentType + ")!");
		}
		return "success";
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
}
多文件上传:
jsp页面
<form enctype="multipart/form-data" action="control/employee/multiUpload.action" method="post">
    文件1:<input type="file" name="images"/>${message0}<br/>
    文件2:<input type="file" name="images"/>${message1}<br/>
    文件3:<input type="file" name="images"/>${message2}<br/>
    <input type="submit" value="上传"/>
</form>
Action
package cn.jbit.struts2.action;

import java.io.File;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionContext;

public class MultiFileUploadAction {

	private File[] images;
	private String[] imagesFileName;
	private String[] imagesContentType;
	
	public String execute() throws IOException {
		// images/
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/images");
		if (null != images) {
			// 目录不存在,则创建目录
			File saveDir = new File(realPath);
			if (!saveDir.exists()) {
				saveDir.mkdirs();
			}
			File saveFile = null;
			for (int i = 0; i < images.length; i++) {
				// 创建保存文件
				saveFile = new File(saveDir, imagesFileName[i]);
				// 文件上传
				FileUtils.copyFile(images[i], saveFile);
				ActionContext.getContext().put("message" + i, "文件上传成功!");
			}
		}
		return "success";
	}

	public File[] getImages() {
		return images;
	}

	public void setImages(File[] images) {
		this.images = images;
	}

	public String[] getImagesFileName() {
		return imagesFileName;
	}

	public void setImagesFileName(String[] imagesFileName) {
		this.imagesFileName = imagesFileName;
	}

	public String[] getImagesContentType() {
		return imagesContentType;
	}

	public void setImagesContentType(String[] imagesContentType) {
		this.imagesContentType = imagesContentType;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值