struts2 多文件上傳

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<head>
<script type="text/javacritp">
function addComponent() 
{
var fileInput = document.createElement("input");
fileInput.setAttribute("type", "file");
fileInput.setAttribute("name", "files");
document.getElementById("field").appendChild(fileInput);
}
</script>
</head>
<body>
<form action="indexAction" method="post" enctype="multipart/form-data">
<div id="field">
<input type="button" onclick="javascript:addComponent();">
<input type="file" name="files">
</div>
</form>
</body>

jsp頁面 javascript

package com.moretv.update.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport {
	private List<File> files;//更新文件集合
	private List<String> filesFileName;//files的文件名集合
	private List<String> filesContentType;//files文件的類型集合
public String index() {
if (files != null) {
			for (int i = 0; i < files.size(); i++) {
				InputStream is;
				OutputStream os;
				try {
					//每一個升級文件的文件名
					String fName = "D:\\"+filesFileName.get(i);
					is = new java.io.FileInputStream(files.get(i));
					os = new java.io.FileOutputStream(fName);
					byte buffer[] = new byte[8192];
					int count = 0;
					while ((count = is.read(buffer)) > 0) {
						os.write(buffer, 0, count);
					}
					os.close();
					is.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
return "SUCCESS";
	}

	public List<String> getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(List<String> filesFileName) {
		this.filesFileName = filesFileName;
	}

	public List<String> getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(List<String> filesContentType) {
		this.filesContentType = filesContentType;
	}
	
	public void setFiles(List<File> files) {
		this.files = files;
	}

	public List<Map> getComVersions() {
		return comVersions;
	}}

action代碼 html

============================== java

在某些狀況下可能發生,jar包沒有問題,代碼沒有問題,可是卻沒法獲取頁面傳來文件,這有多是缺乏了文件上傳攔截器的緣故。例子以下: app

<action name="add" class="AAction" method="add">
			<result name="success" type="redirect">/A/list.action</result>
			<interceptor-ref name="fileUploadStack"></interceptor-ref>
		</action>
相關文章
相關標籤/搜索