(struts2学习篇)struts2文件上传

虾米哥 阅读:574 2021-04-01 10:26:34 评论:0

第一步:编写相关相关文件上传Action

public class UploadFileAction extends ActionSupport { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
	// 相关属性封装 
	private File upload; // 上传文件 
 
	private String uploadContentType;// 上传文件类型 
	private String uploadFileName;// 上传文件名 
 
	private String filename;// 上传文件重新命名 
 
	private String upladPath;// 上传文件路径 
 
	private String result;// 上传结果 
 
	public File getUpload() { 
		return upload; 
	} 
 
	public void setUpload(File upload) { 
		this.upload = upload; 
	} 
 
	public String getUploadContentType() { 
		return uploadContentType; 
	} 
 
	public void setUploadContentType(String uploadContentType) { 
		this.uploadContentType = uploadContentType; 
	} 
 
	public String getUploadFileName() { 
		return uploadFileName; 
	} 
 
	public void setUploadFileName(String uploadFileName) { 
		this.uploadFileName = uploadFileName; 
	} 
 
	public String getFilename() { 
		return filename; 
	} 
 
	public void setFilename(String filename) { 
		this.filename = filename; 
	} 
 
	public String getUpladPath() { 
		return upladPath; 
	} 
 
	public void setUpladPath(String upladPath) { 
		this.upladPath = upladPath; 
	} 
 
	@Override 
	public String execute() throws Exception { 
		// TODO Auto-generated method stub 
		String file = ""; 
		// 如果新文件名未输入,则使用上传文件的文件名,做为服务器保存文件的文件名 
		if (filename.equals("")) { 
			file = upladPath + uploadFileName; 
		} else { 
			file = upladPath + uploadFileName; 
		} 
		// 判读服务器是否存在同名的文件,否则相关的信息提示 
		if (new File(file).exists()) { 
			result = "该文件已经存在,请为文件重新命名"; 
		} else { 
			FileOutputStream fileoutput = new FileOutputStream(file); 
			InputStream input = new FileInputStream(upload); 
			byte[] buffer = new byte[8192]; 
			int count = 0; 
			while ((count = input.read(buffer)) > 0) { 
				fileoutput.write(buffer, 0, count); 
			} 
			fileoutput.close(); 
			input.close(); 
			result = "文件上传成功"; 
		} 
		return "result"; 
	} 
 
	public void setResult(String result) { 
		this.result = result; 
	} 
 
	public String getResult() { 
		return result; 
	} 
}


第二步:struts2.xml文件配置

	<!--struts2文件上传  --> 
	<package name="fileupload" extends="struts-default"> 
	<action name="fileupload" class="com.rf.action.UploadFileAction"> 
	<!--struts2 服务器保存上传文件  --> 
	<param name="upladPath">c:\upload\</param> 
	<result name="result">/error.jsp</result>	 
	</action> 
	</package>


 

第三步:jsp页面相关代码(文件上传页面)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<%@taglib prefix="s" uri="/struts-tags"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head>     
    <title>struts2文件上传实例代码</title>	 
	<!-- 
	<link rel="stylesheet" type="text/css" href="styles.css"> 
	--> 
  </head>   
  <body> 
  <!--struts2文件上传  --> 
  <s:form action="fileupload" enctype="multipart/form-data"> 
     <s:file label="文件上传" name="upload"/> 
     <s:textfield label="新文件名" name="filename"/> 
     <s:submit value="上传"/> 
  </s:form> 
  </body> 
</html>


 

结果页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<%@taglib prefix="s" uri="/struts-tags"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head>     
    <title>上传文件结果</title> 
   <!-- 
	<link rel="stylesheet" type="text/css" href="styles.css"> 
	--> 
  </head>   
  <body> 
 	 文件上传结果: 
       <s:property value="result"/><br> 
  </body> 
</html> 


结果展示:

标签:Struts2
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号