struts2 文件上传

2018-08-27 11:15:08 浏览数 (1)

upload.jsp

代码语言:javascript复制
<%@ 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

代码语言:javascript复制
<?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

代码语言:javascript复制
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 人点赞