【第22期】观点:IT 行业加班,到底有没有价值?

struts2 文件上传

原创 2013年12月03日 20:08:18

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;
	}
}	



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Struts2实现文件上传和下载

在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来...

struts2单文件上传

文件上传几乎是所有web应用实现的一个必须模块。文件上传的实现需要将表单元素属性enctype的值设置为multipart/form-data,使表单数据以二进制编码方式提交。

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

Struts2的文件上传和下载

通过Struts.xml来配置 架包 文件上传下载JSP页面 Insert title here /* 当上传上去的文件不符合要求时,返回该界面的错误提示 */ ...

struts2_文件上传

struts2文件上传操作。

struts2之单个文件上传

通过2种方式模拟单个文件上传,效果如下所示 开发步骤如下: 1、新建一个web工程,导入struts2上传文件所需jar,如下图 目录结构   ...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)