Struts2.0学习笔记---上传文件和多文件上传

29 篇文章 0 订阅



粘上自己的代码:

ShowWords.java

package action;

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

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


public class ShowWords{
	private File uploadImage;
	private String uploadImageContentType;
	private String uploadImageFileName;
	
	public File getUploadImage() {
		return uploadImage;
	}
	public void setUploadImage(File uploadImage) {
		this.uploadImage = uploadImage;
	}
	public String getUploadImageContentType() {
		return uploadImageContentType;
	}
	public void setUploadImageContentType(String uploadImageContentType) {
		this.uploadImageContentType = uploadImageContentType;
	}
	public String getUploadImageFileName() {
		return uploadImageFileName;
	}
	public void setUploadImageFileName(String uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}
	public String execute() {
		String realpath = ServletActionContext.getServletContext().getRealPath("/image");
		File f=new File(realpath);
		if(!f.exists())
		{ f.mkdirs(); }
		try {
			FileUtils.copyFile(uploadImage,new File(f,uploadImageFileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
}

inf.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'inf.jsp' starting page</title>
  </head>
  
  <body>
   <form  action="showWords.action"   method="post"  enctype="multipart/form-data">
   	    文件: <input type="file"  name="uploadImage">
   		<input type="submit" name="提交">
   </form>
  </body>
</html>
要注意:form表单name属性要与ShowWords中的File  uploadImage名称相同。



多文件上传,就是要把    uploadImage  uploadImageFileName改为数组,原来form表单上几个同时提交的数据的名称要设为一致的。

代码:

ShowWords.java

package action;

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

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


public class ShowWords{
	private File[] uploadImage;
	private String[] uploadImageFileName;
	
	public File[] getUploadImage() {
		return uploadImage;
	}
	public void setUploadImage(File[] uploadImage) {
		this.uploadImage = uploadImage;
	}
	public String[] getUploadImageFileName() {
		return uploadImageFileName;
	}


	public void setUploadImageFileName(String[] uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}


	public String execute() {
		String realpath = ServletActionContext.getServletContext().getRealPath("/image");
		File f=new File(realpath);
		if(!f.exists())
		{ f.mkdirs(); }
		try {
			for(int i=0;i<uploadImage.length;++i)
			{
				FileUtils.copyFile(uploadImage[i],new File(f,uploadImageFileName[i]));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
}
inf.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'inf.jsp' starting page</title>
  </head>
  
  <body>
   <form  action="showWords.action"   method="post"  enctype="multipart/form-data">
   	    文件: <input type="file"  name="uploadImage"><br/>
   	    文件: <input type="file"  name="uploadImage"><br/>
   		<input type="submit" name="提交">
   </form>
  </body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值