`
zwdsmileface
  • 浏览: 152548 次
社区版块
存档分类
最新评论

Struts2 文件上传

阅读更多
在struts2中实现文件的上传(采用Struts2标签)
首先是jsp页面
文件上传界面:UpFile.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'UpFile.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>
	<s:form action="fileUpload.action" method="post" enctype="multipart/form-data" >
	<s:file name="myFile" label="MyFile" ></s:file>
	<s:textfield name="caption" label="Caption"></s:textfield>
	<s:submit label="提交"></s:submit>
	</s:form>
  </body>
</html>

文件显示界面:ShowUpLoad.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'ShowUpload.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 style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
		<img src ="UploadImages/<s:property value ="imageFileName"/> "/>

		<s:property value ="caption"/>
	</div >
</body>
</html>
后台程序 FileUploadAction.java
public class FileUploadAction extends ActionSupport {
	private static final int BUFFER_SIZE = 16 * 1024 ;
	//注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
	//所以同时要提供myFileContentType,myFileFileName的set方法
	private File myFile; //上传文件
	private String contentType;//上传文件类型
	private String fileName; //上传文件名
	private String imageFileName;
	private String caption;//文件说明,与页面属性绑定

	public void setMyFileContentType(String contentType) {
	System.out.println("文件类型 : " + contentType);
	this .contentType = contentType;
	}

	public void setMyFileFileName(String fileName) {
	System.out.println("文件名称 : " + fileName);
	this .fileName = fileName;
	}

	public void setMyFile(File myFile) {
	this .myFile = myFile;
	}

	public String getImageFileName() {
	return imageFileName;
	}

	public String getCaption() {
	return caption;
	}

	public void setCaption(String caption) {
	this .caption = caption;
	}

	private static void copy(File src, File dst) {
	try {
	InputStream in = null ;
	OutputStream out = null ;
	try {
	in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
	out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
	byte [] buffer = new byte [BUFFER_SIZE];
	while (in.read(buffer) > 0 ) {
		out.write(buffer);
	}
	} finally {
	if ( null != in) {
		in.close();
	}
	if ( null != out) {
		out.close();
	}
	}
	} catch (Exception e) {
		e.printStackTrace();
	}
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	@Override
	public String execute() {
	imageFileName = new Date().getTime() + getExtention(fileName);
	File imageFile = new File(ServletActionContext.getServletContext().getRealPath("UploadImages" ) + "/" + imageFileName);//注意UploadImage文件必须存在,否则将存储不成功,也可以自己建立其他文件夹存储上传文件
	System.out.println(imageFile.getPath());
	copy(myFile, imageFile);
	return SUCCESS;
	}
}
配置Struts.xml信息:
<action name="fileUpload" class="action.FileUploadAction">
			<interceptor-ref name="fileUploadStack"/>
			<result>/ShowUpload.jsp</result>
		</action>
0
0
分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2文件上传

    struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2文件上传

    Struts2文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

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

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics