`

webwork上传多个文件

阅读更多

版本:webwork2.2.5

 

各配置文件与《webwork上传单个文件》中的一直,现只写uploadFiles.jsp和uploadAction2.java

 

uploadFiles.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
	    <title>多文件上传实例</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">    
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
		<link rel="stylesheet" type="text/css" href="styles.css">
		-->
		
		<script language="Javascript">
		    function addAccessory() {
		        var accessoryListObj = document.getElementById("accessoryList");
		        var accObj = document.createElement("span");
		        var spanId = (new Date()).getTime();
		        var spanHTML = "<input type='file' name='files' size='35'><input type='button' value='删除' onclick=deleteFile('" + spanId + "')><br>";
		
		        accObj.setAttribute("id", spanId);
		        accObj.setAttribute("width", "500px");
		        accObj.innerHTML = spanHTML;
		        accessoryListObj.appendChild(accObj);
		    }

		    function deleteFile(fileId) {
		        var accessoryListObj = document.getElementById("accessoryList");
		        var accObj = document.getElementById(fileId);
		        accessoryListObj.removeChild(accObj);
		    }
		</script>
	</head>
	<body>
   		<form name="form1" method="post" enctype="multipart/form-data" action="member/upload2.do"> 
 			<table id="DataTable">
            	<tbody>
                	<tr>
                    	<td colspan="4"><strong>附件:</strong>
                        	<span onclick="addAccessory()" style="cursor:pointer;color:blue;">
                        		<strong>[添加附件]</strong>
                        	</span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="width:500px;height:200px;">
                        	<div id="accessoryList" style="overflow-x:hidden;overflow-y:auto;width:100%;height:100%;">
                            	&nbsp;
                            </div>
                        </td>
                    </tr>
                    <tr>
                    	<td colspan="4" style="width:500px;height:15px;" align="center">
                        	<input type="submit" value="提交">            
                        </td>
                    </tr>
                </tbody>
            </table>
   		</form>
	</body>
</html>

 

 

 

 

uploadAction2.java

package com.luodada.trans.soa.busi.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import com.opensymphony.xwork.ActionSupport;

public class UploadAction2 extends ActionSupport {

	private File[] files;
	private String[] filesFileName;
	
	public String uploadFile() {
		upload();
		return "succ";
	}
	
	public void upload() {
		if (files != null) {
			for (int i = 0 ; i < files.length ; i++) {
				FileOutputStream outputStream; 
				try{ 
					ResourceBundle rb = ResourceBundle.getBundle("config");
					String fileDir = rb.getString("saveDir") + File.separator;
					Date date = new Date();
					DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
					String time = df.format(date);
					filesFileName[i] = time + "_" + filesFileName[i];
					String filePath = fileDir + filesFileName[i];
					try {
						// 创建目录 
						File f = new File(fileDir);   
						f.mkdirs(); 
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					outputStream = new FileOutputStream(filePath); 
					FileInputStream fileIn = new FileInputStream(files[i]); 
					byte[] buffer = new byte[1024]; 
					int len; 
					while ((len = fileIn.read(buffer)) > 0) { 
						outputStream.write(buffer, 0, len); 
					} 
					fileIn.close(); 
					outputStream.close();
				} catch (FileNotFoundException ex) { 
					ex.printStackTrace(); 
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				System.out.println("uploadfile name="+filesFileName[i]);
			}
		} else {
			System.out.println("file is null!");
		}
	}
	
	public File[] getFiles() {
		return files;
	}
	public void setFiles(File[] files) {
		this.files = files;
	}
	public String[] getFilesFileName() {
		return filesFileName;
	}
	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics