struts2上傳多個文件
A. struts2上傳多個文件時struts.xml中怎麼寫
上傳下載都有。。。僅僅作為參考。有問題可以追問。
<struts>
  <constant name="struts.custom.i18n.resources" value="globalMessages" /> 
  <constant name="struts.custom.i18n.Encoding" value="GBK" /> 
  <constant name="strurs.devMode" value="true" /> 
  <constant name="struts.multipart.saveDir" value="e:/temp" /> 
- <package name="default" extends="struts-default">
- <action name="upload" class="com.bbs.upload.action.UploadAction">
  <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> 
  <param name="savePath">/upload</param> 
  <result name="input">/error.jsp</result> 
  <result name="success">/success.jsp</result> 
  </action>
- <action name="download" class="com.bbs.upload.action.DownloadAction">
  <param name="fileName" /> 
- <result name="success" type="stream">
- <!--   <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> 
  --> 
- <!--    <param name="contentType">text/plain</param>
  --> 
  <param name="inputName=">targetFile</param> 
  <param name="contentDisposition">fileName="bg.png"</param> 
  <param name="inputName">downloadFile</param> 
  </result>
  </action>
- <action name="*">
  <result>/{1}.jsp</result> 
  </action>
  </package>
  </struts>
B. struts2 如何實現上傳整個文件夾的功能
一、壓縮文件其實是可以0壓縮率直接打包,這樣其實蠻快的
二、看到網上說Applet可以上傳文件夾,具體遠離不清楚,你可以看看
三、最笨的方法,用Ajax做一個遞歸遍歷文件夾的函數,如果是文件就上傳上去,如果是文件夾就請求後台新建文件夾
四、用JSON格式把目錄和文件封裝起來,統一傳到後台,但是後台處理要比較麻煩
C. struts2批量上傳文件提示以下異常
struts.multipart.saveDir沒有配置。
struts.multipart.saveDir用於指定存放臨時文件的文件夾,
於是在,struts.xml配置文件中增加如下:
<constant
name="struts.multipart.saveDir"
value="/tmp"/>
設置臨時文件上傳路徑。
D. struts2 在jsp一個上傳框如何實現全選多個文件一次上傳呢
jsp代碼
 <s:form action="uploadAction!uploadFilemuch.action" method="post" enctype="multipart/form-data" >
    File1:<s:file name="uploadmuch" label="selectfile"></s:file>
    File2:<s:file name="uploadmuch" label="selectfile"></s:file>
    File3:<s:file name="uploadmuch" label="selectfile"></s:file>
    <s:submit value="submit" />
</s:form>
action代碼
public class UpLoadFiled extends ActionSupport {
	private File upload;
	private String uploadFileName;
	private File[] uploadmuch;
	private String[] uploadmuchFileName;
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public File[] getUploadmuch() {
		return uploadmuch;
	}
	public void setUploadmuch(File[] uploadmuch) {
		this.uploadmuch = uploadmuch;
	}
	public String[] getUploadmuchFileName() {
		return uploadmuchFileName;
	}
	public void setUploadmuchFileName(String[] uploadmuchFileName) {
		this.uploadmuchFileName = uploadmuchFileName;
	}
public String uploadFilemuch() throws IOException {
		if (this.getUploadmuch() != null) {
			for (int i = 0; i < this.getUploadmuch().length; i++) {
				FileOutputStream fos = new FileOutputStream(
						ServletActionContext.getRequest().getRealPath(
								"\\upload")
								+ "\\" + this.getUploadmuchFileName()[i]);
				FileInputStream fis = new FileInputStream(this.getUploadmuch()[i]);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
			}
		}
		ActionContext.getContext().put("message", "上傳成功");
		Log.addLog("上傳成功");
		return "message";
	}
}
E. Struts2上傳多個文件並寫入資料庫
一般情況下,完成上傳文件後把文件的路徑保存到資料庫中就可以了,o(∩_∩)o~
F. struts2可以上傳整個文件夾(裡面全是圖片)嗎求指點
可以 ,只要你將文件類型制定好就可以了,但是你必須想將其壓縮
例如<param name="allowedTypes">file/jar,file/zip</param>
G. Struts2 多文件上傳,高手請進!!
首先,你要知道怎麼在action是區分文件。struts2可以同時上傳多文件,action接收到的是一個File list,如果你要區分只能從文件的contentType進行差別,但不同瀏覽器對同一種文件類型的contentType有可能不同。所以,你要知道不同瀏覽器對文件的contentType。
最後,就是在action里得到得到文件的contentType進行判斷,對不同的文件進行不同處理就行了。
當然你也可以用文件的後綴進行判斷,不過不推薦使用後綴進行文件類型的判斷
H. java中怎麼利用struts2上傳多個pdf文件
通過3種方式模擬多個文件上傳 
第一種方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
    private File[] image; //上傳的文件
    private String[] imageFileName; //文件名稱
    private String[] imageContentType; //文件類型
    public String execute() throws Exception {
        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        System.out.println(realpath);
        if (image != null) {
            File savedir=new File(realpath);
            if(!savedir.getParentFile().exists())
                savedir.getParentFile().mkdirs();
            for(int i=0;i<image.length;i++){
                File savefile = new File(savedir, imageFileName[i]);
                FileUtils.File(image[i], savefile);
            }
            ActionContext.getContext().put("message", "文件上傳成功");
        }
        return "success";
    }
    public File[] getImage() {
        return image;
    }
    public void setImage(File[] image) {
        this.image = image;
    }
    public String[] getImageContentType() {
        return imageContentType;
    }
    public void setImageContentType(String[] imageContentType) {
        this.imageContentType = imageContentType;
    }
    public String[] getImageFileName() {
        return imageFileName;
    }
    public void setImageFileName(String[] imageFileName) {
        this.imageFileName = imageFileName;
    }
}
第二種方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 使用數組上傳多個文件
 * 
 * @author ljq
 *
 */
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport{
    private File[] image; //上傳的文件
    private String[] imageFileName; //文件名稱
    private String[] imageContentType; //文件類型
    private String savePath;
    @Override
    public String execute() throws Exception {
        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
        //取得需要上傳的文件數組
        File[] files = getImage();
        if (files !=null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                //建立上傳文件的輸出流, getImageFileName()[i]
                System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
                FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
                //建立上傳文件的輸入流
                FileInputStream fis = new FileInputStream(files[i]);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len=fis.read(buffer))>0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                fis.close();
            }
        }
        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;
    }
    /**
     * 返回上傳文件保存的位置
     * 
     * @return
     * @throws Exception
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
第三種方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 使用List上傳多個文件
 * 
 * @author ljq
 *
 */
@SuppressWarnings("serial")
public class UploadAction3 extends ActionSupport {
    private List<File> image; // 上傳的文件
    private List<String> imageFileName; // 文件名稱
    private List<String> imageContentType; // 文件類型
    private String savePath;
    @Override
    public String execute() throws Exception {
        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
        // 取得需要上傳的文件數組
        List<File> files = getImage();
        if (files != null && files.size() > 0) {
            for (int i = 0; i < files.size(); i++) {
                FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().get(i));
                FileInputStream fis = new FileInputStream(files.get(i));
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fis.close();
                fos.close();
            }
        }
        return SUCCESS;
    }
    public List<File> getImage() {
        return image;
    }
    public void setImage(List<File> image) {
        this.image = image;
    }
    public List<String> getImageFileName() {
        return imageFileName;
    }
    public void setImageFileName(List<String> imageFileName) {
        this.imageFileName = imageFileName;
    }
    public List<String> getImageContentType() {
        return imageContentType;
    }
    public void setImageContentType(List<String> imageContentType) {
        this.imageContentType = imageContentType;
    }
    /**
     * 返回上傳文件保存的位置
     * 
     * @return
     * @throws Exception
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
struts.xml配置文件
<?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>
    <!-- 該屬性指定需要Struts2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
        如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。 -->
    <constant name="struts.action.extension" value="do" />
    <!-- 設置瀏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 當struts的配置文件修改後,系統是否自動重新載入該文件,默認值為false(生產環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 開發模式下使用,這樣可以列印出更詳細的錯誤信息 -->
    <constant name="struts.devMode" value="true" />
    <!-- 默認的視圖主題 -->
    <constant name="struts.ui.theme" value="simple" />
    <!--<constant name="struts.objectFactory" value="spring" />-->
    <!--解決亂碼    -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.multipart.maxSize" value="10701096"/>
    <package name="upload" namespace="/upload" extends="struts-default">
        <action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>
    <package name="upload1" namespace="/upload1" extends="struts-default">
        <action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
            <!-- 要創建/image文件夾,否則會報找不到文件 -->
            <param name="savePath">/image</param>
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>
    <package name="upload2" namespace="/upload2" extends="struts-default">
        <action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
            <!-- 要創建/image文件夾,否則會報找不到文件 -->
            <param name="savePath">/image</param>
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>
</struts>
上傳表單頁面upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>文件上傳</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
    </head>
    <body>
        <!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
        <!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
        <!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
        <!--  -->
        <form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
            文件1:<input type="file" name="image"><br/>
            文件2:<input type="file" name="image"><br/>
            文件3:<input type="file" name="image"><br/>
                <input type="submit" value="上傳" />
        </form>
    </body>
</html>
顯示頁面message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'message.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  <body>
    上傳成功
  <br/>
  <s:debug></s:debug>
  </body>
</html>
I. struts2如何同時上傳文件以及獲得表單數據
事實上這根本不需要什麼其他配置操作,因為這是Struts2,而不是原生Servlet,在Struts2中,攔截器會將request中的表單數據(或者文件格式的數據)都和action類中的屬性名稱一一對應的注入值(包括文件數據)。所以你需要做的,其實只是在jsp頁面(或html)中加入一個file類型的input標簽,名稱記住(比如為photo),然後在action類中加一個File類型(java.io.File)欄位,此欄位必須和剛剛的input標簽name屬性一致,即photo(private File photo;)。最後,需要注意的是,當你妄圖從網頁上傳一個文件類型的表單時,必須將包圍它的form類將enctype="multipart/form-data" method="post"加上,即method必須為post,且enctype,也就是表單數據類型,必須為二進制的。
J. struts2批量上傳的問題
1、import org.omg.CORBA.Request; 這個類不是Servlet的Request
2、沒看到你對javax.servlet.ServletContext的導入,語法上一定會報錯的
