struts2批量上传图片

jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>批量上传图片</title>

</head>
<script type="text/javascript">
var num = 3;
function addMoreFiles(){
	//添加一行
    var newTr = myTable.insertRow();
    //添加两列
    var newTd0 = newTr.insertCell();
    var newTd1 = newTr.insertCell();
    var newTd2 = newTr.insertCell();
    //设置列内容和属性
    newTd0.innerText = '图片'+num+':'; 
    newTd1.innerHTML= '<input type="file" name="file" value="" id="file"/>';
    newTd2.innerHTML='<input type="button" name="delete" value="删除" id="delete" οnclick="delFile();"/>'
	num++;
}

function delFile(){
	var oRow=event.srcElement.parentNode.parentNode;
    var oTable=oRow.parentNode.parentNode;
   	if(oTable.rows.length>1){
        oTable.deleteRow(oRow.rowIndex);
        if(oTable.rows.length==1)
        {
           oTable.all("delete").disabled=true;
        }
    }
}
</script>

<body>

<form id="" enctype="multipart/form-data" method="post" action="uploadImageAction.action">
	<input type="hidden" id="luJing" name="luJing" value="${info.baoCunZhuLuJing }">
	<table id="myTable">
		<tr>
			<td width="70px;">图片1:</td>
			<td><input type="file" name="file" value="" id="file"/></td>
			<td><input type="button" name="delete" value="删除" id="delete" οnclick="delFile();"/></td>
		</tr>
		<tr>
			<td width="70px;">图片2:</td>
			<td><input type="file" name="file" value="" id="file"/></td>
			<td><input type="button" name="delete" value="删除" id="delete" οnclick="delFile();"/></td>
		</tr>
	</table>
    <input type="submit" id="upload" name="upload" value="上传"/>
	<input type="button" value="上传更多" οnclick="addMoreFiles();">
	    
</form>
</body>

</html>


struts配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <constant name= "struts.multipart.maxSize" value="1073741824" />
   <package name="default" extends="struts-default">
   		<action name="uploadImageAction" class="xu.hunSha.managerment.UploadImageAction">
			<result name="success">/success.jsp</result>
			 <result name="error">/error.jsp</result>
			<result name="input">/error.jsp</result>			
			<interceptor-ref name="fileUpload">
				<!-- 配置允许上传的文件大小300M 314572800-->
				<param name="maximumSize">31457280</param>   
			</interceptor-ref>   
			
			<interceptor-ref name="defaultStack"/>
		</action>
   </package>
</struts>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UploadImageAction extends ActionSupport implements Action{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8658947212993204626L;
	
	private List<File> file;
    private List<String> fileFileName;
    private List<String> fileContentType; 
    private String luJing;
    
    public String execute() throws IOException{
    //得到工程保存图片的路径
    	String root = ServletActionContext.getServletContext().getRealPath("/");
    	//创建文件夹
    	File dirs = new File(root+luJing);
    	if(!dirs.exists()){
    		dirs.mkdirs();
    	}
        //循环上传的文件
    	if(file == null){
    		return ERROR;
    	}
        for(int i = 0 ; i < file.size() ; i ++){
            InputStream is = new FileInputStream(file.get(i));
            
            //得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
            File destFile = new File(root+luJing,this.getFileFileName().get(i));
            //System.out.println(root+luJing);
            //把图片写入到上面设置的路径里
            OutputStream os = new FileOutputStream(destFile);
            Long leng =  file.get(i).length();
            byte[] buffer = new byte[leng.intValue()];
            int length  = 0 ;
            while((length = is.read(buffer))>0){
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
        }
        return SUCCESS;
    }

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getLuJing() {
		return luJing;
	}

	public void setLuJing(String luJing) {
		this.luJing = luJing;
	}

}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值