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

首頁 > 學院 > 開發設計 > 正文

關于struts2文件上傳的記錄

2019-11-11 04:58:09
字體:
來源:轉載
供稿:網友

         要進行struts2文件上傳,我們需要寫一個action類,類中需要定義一個需要上傳的文件變量,即 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。

        上傳頁面代碼如下:

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

為必須項,否則無法進行文件的上傳。

        當缺少 method="post" 屬性時,會報錯如下:

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文件上傳后的臨時存放路徑。

        注:在實際的操作中發現,我們可以不用在action類中定義upFileContentType和upFileFileName字段,可以根據需要自己添加。

      


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舒城县| 克山县| 赞皇县| 昌平区| 云南省| 时尚| 卢氏县| 于田县| 巴彦县| 兴义市| 新邵县| 宾阳县| 汉源县| 定远县| 淅川县| 从江县| 花莲县| 河北区| 英吉沙县| 新巴尔虎左旗| 新营市| 札达县| 长宁县| 广宁县| 达孜县| 连云港市| 潞城市| 吉安市| 措勤县| 陇南市| 南丹县| 阿城市| 石门县| 英德市| 林周县| 濉溪县| 新营市| 密云县| 手游| 简阳市| 昂仁县|