前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >struts2 文件上传

struts2 文件上传

作者头像
week
发布2018-08-27 11:15:08
3680
发布2018-08-27 11:15:08
举报
文章被收录于专栏:用户画像用户画像

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;
	}
}	
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年12月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档