`
z_xiaofei168
  • 浏览: 198036 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2详解(二)---->>单个文件上传

阅读更多

struts2详解(二)---->>单个文件上传

实现原理:
      Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。

具体实现
    先要引入必要的jar包:

 

1、首先,创建文件上传页面index.jsp,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.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">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>
	<body>
		<div align="center">
			<!-- enctype="multipart/form-data" 这个属性是必须得有的 -->
			<form action="csdn/upload.action" method="post"
				enctype="multipart/form-data">
				<table border="1px">
					<tr>
						<td>
							标题:
						</td>
						<td>
							<input type="text" name="title" />
						</td>
					</tr>
					<tr>
						<td>
							文件:
						</td>
						<td>
							<input type="file" name="pic" />
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center">
							<input type="submit" value="上传" />
						</td>
					</tr>
				</table>
			</form>

		</div>
	</body>
</html>

 

 

备注:
    在index.jsp中,先将表单的提交方式设为POST,然后将enctype设为multipart/form-data

 

2、其次是UserAction.java代码

package cn.csdn.struts2.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	/**
	 * @author z_xiaofei168
	 */
	private static final long serialVersionUID = 1L;
	
	/** 标题 */
	private String title;
	/** 获取文件   文件名与视图层的名称一致 */
	private File pic;
	/** 获取文件的类型     必须是:  文件名+ContentType */
	private String picContentType;
	/** 获取文件的名称     必须是:   文件名+FileName  */
	private String picFileName;
	/** 保存文件路径      /保存文件的文件名称  */
	private String savePath;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getPic() {
		return pic;
	}
	public void setPic(File pic) {
		this.pic = pic;
	}
	public String getPicContentType() {
		return picContentType;
	}
	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}
	public String getPicFileName() {
		return picFileName;
	}
	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}
	public String getSavePath() {
		/**获取上下的路径*/
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
	/** copy读取  */
	public String uploadFile(){
		System.out.println("savePath:"+savePath);
		System.out.println("pic:"+pic);
		System.out.println("picContentType:"+picContentType);
		System.out.println("picFileName:"+picFileName);
		System.out.println("getSavePath():"+getSavePath());
		/**保存的具体路径*/
		String savepath = getSavePath();
		/**根据保存的路径创建file对象*/
		File file = new File(savepath);
		if(!file.exists()){
			/**创建此文件对象路径*/
			file.mkdirs();
		}
		try {
			/**使用的是:org.apache.commons.io.FileUtils FileUtils*/
			FileUtils.copyFile(pic, new File(file,getPicFileName()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}

  

3、配置文件: struts.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
     <package name="csdn" extends="struts-default" namespace="/csdn">
     	<global-results>
     		<result name="input">/index.jsp</result>
     	</global-results>
     	
     	<action name="upload" class="cn.csdn.struts2.action.UserAction" method="uploadFile">
     		<!-- 文件保存的文件名称 -->
     		<param name="savePath">/uploads</param>
     		<result name="success">/sc.jsp</result>
     	</action>
     </package>   
</struts>

 

 

4、下面我们就来看看上传成功的页面:

 

5
6
分享到:
评论
2 楼 z_xiaofei168 2011-04-29  
huangsky 写道
有没有这样的方法满足这样的需求?

当上传文件大小限制为10M
上传的文件大小大于10M的时候,这文件一传就能马上提示,而不用等10M传完了再提示超过大小?
就像现在的邮箱上传附件一样,能马上提示

你可以使用ajax的异步交互,先获取文件的大小,和你的那个系统的默认文件做比较,即可。
1 楼 huangsky 2011-04-28  
有没有这样的方法满足这样的需求?

当上传文件大小限制为10M
上传的文件大小大于10M的时候,这文件一传就能马上提示,而不用等10M传完了再提示超过大小?
就像现在的邮箱上传附件一样,能马上提示

相关推荐

Global site tag (gtag.js) - Google Analytics