国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

關(guān)于struts2文件上傳的記錄

2019-11-11 06:26:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

         要進(jìn)行struts2文件上傳,我們需要寫一個(gè)action類,類中需要定義一個(gè)需要上傳的文件變量,即 PRivate File upFile;

      action類中代碼如下:

package struts.action;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 javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadDemo extends ActionSupport {	private File upFile;	private String uploadPath;	private String upFileContentType;// 文件類型	private String upFileFileName; // 文件名	public File getUpFile() {		return upFile;	}	public void setUpFile(File upFile) {		this.upFile = upFile;	}	public String getUploadPath() {		return uploadPath;	}	public void setUploadPath(String uploadPath) {		this.uploadPath = uploadPath;	}	public String getUpFileContentType() {		return upFileContentType;	}	public void setUpFileContentType(String upFileContentType) {		this.upFileContentType = upFileContentType;	}	public String getUpFileFileName() {		return upFileFileName;	}	public void setUpFileFileName(String upFileFileName) {		this.upFileFileName = upFileFileName;	}	@Override	public String execute() throws Exception {		ServletContext context = ServletActionContext.getServletContext();		String realPath = context.getRealPath(uploadPath);		InputStream is = null;		OutputStream os = null;		try {			is = new FileInputStream(upFile);			File file=new File(realPath+"/"+upFileFileName);			os = new FileOutputStream(file);			int len = 0;			byte[] buf = new byte[2048];			while ((len = is.read(buf)) > 0) {				os.write(buf, 0, len);			}		} catch (IOException e) {			e.printStackTrace();		} finally {			if (is != null)				is.close();			if (os != null)				os.close();		}		return SUCCESS;	}}      其中定義的upFileContentType和upFileFileName字段的前綴upFile必須是定義的文件File的變量名upFile。

      action類中的uploadPath在struts2.xml文件中可以定義如下:

<action name="FileUploadDemo" class="struts.action.FileUploadDemo"			method="execute">		<param name="uploadPath">/upload</param>		<result name="success">/demo.jsp</result>	</action>        這樣,我們就可以在action類中直接使用uploadPath,賦值即為upload。

        上傳頁(yè)面代碼如下:

<form name="uploadForm" id="uploadForm" enctype="multipart/form-data" method="post" action="FileUploadDemo.ac" >	<input name="upFile" type="file"/>	<input type="submit"  value="上傳"/></form>        在頁(yè)面中
enctype="multipart/form-data" method="post"

為必須項(xiàng),否則無(wú)法進(jìn)行文件的上傳。

        當(dāng)缺少 method="post" 屬性時(shí),會(huì)報(bào)錯(cuò)如下:

Struts Problem ReportStruts has detected an unhandled exception:Messages: 	No result defined for action struts.action.FileUploadDemo and result inputFile: 	file:/D:/Eclipse/Workspace/CombOfThreeFrameWorks/WebRoot/WEB-INF/classes/struts.xmlLine number: 	22Column number: 	21

        可以定義struts.properties文件

struts.multipart.saveDir=D://Eclipse//Workspace//CombOfThreeFrameWorks        該文件定義為upFile文件上傳后的臨時(shí)存放路徑。

        注:在實(shí)際的操作中發(fā)現(xiàn),我們可以不用在action類中定義upFileContentType和upFileFileName字段,可以根據(jù)需要自己添加。

      


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 康平县| 太白县| 尖扎县| 昭平县| 揭阳市| 富平县| 泸定县| 句容市| 阿勒泰市| 惠水县| 巴彦淖尔市| 濉溪县| 延庆县| 龙门县| 德格县| 松桃| 河津市| 翁源县| 长子县| 广西| 鄂托克前旗| 夏邑县| 南昌市| 富裕县| 永吉县| 洛浦县| 肥东县| 富平县| 梁河县| 靖宇县| 凤台县| 天台县| 虞城县| 平舆县| 大英县| 蒙阴县| 绥滨县| 阳信县| 沧源| 崇文区| 通许县|