关闭

struts2 文件上传

384人阅读 评论(0) 收藏 举报
分类:

upload.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>上传</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">

</head>

<body>
	<div id="content">
		<div id="infowrap">
			<div id="box">
				<h3>上传Excel工资表</h3>
				<s:form action="file_upload" method="post"
					enctype="multipart/form-data" namespace='/file'>
					<s:file name="upload" label="上传的文件"></s:file>
					<s:submit value="上传"></s:submit>
					<s:hidden name="uploadPath" value="Files/salary"></s:hidden>
				</s:form>
			</div>
		</div>
	</div>
</body>
</html>
struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.multipart.saveDir" value="/files"></constant>
	<package name="file" extends="struts-default" namespace="/file">
		<action name="file_*" method="{1}" class="edu.qdgxy.action.FileAction">
			<result name="upload">/pages/back/admin_frame.jsp</result>
		</action>
	</package>
</struts>

FileAction.java

package edu.qdgxy.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import jxl.Cell;
import org.apache.struts2.ServletActionContext;
import edu.qdgxy.util.ExcelDele;
import edu.qdgxy.util.ExcelRead;

public class FileAction extends SuperAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File upload;
	private String uploadFileName;
	private String uploadPath;
	public String upload() throws Exception{
		ExcelDele excelDele = new ExcelDele();	
		InputStream is=new FileInputStream(getUpload());
		System.out.println(uploadPath);
		String path=ServletActionContext.getServletContext().getRealPath(uploadPath);
		OutputStream os=new FileOutputStream(path+"/"+uploadFileName);
		
		byte buffer[]=new byte[1024];
		int cnt;
		while((cnt=is.read(buffer))>0){
			os.write(buffer,0,cnt);
		}
		os.close();
		is.close();
		
		pages="upload_success.jsp";
		request.put("pages",pages);
		
		return "upload";
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadPath() {
		return uploadPath;
	}
	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}
}	



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:475136次
    • 积分:8565
    • 等级:
    • 排名:第2388名
    • 原创:412篇
    • 转载:24篇
    • 译文:3篇
    • 评论:102条
    最新评论