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

struts2 文件上传

作者头像
week
发布2018-08-27 09:30:56
2810
发布2018-08-27 09:30:56
举报
文章被收录于专栏:用户画像用户画像

一、文件上传页面

uploadPath是相对webroot的路径,即webroot下的路径,将文件上传至该文件夹下。

代码语言: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工资表,版本为*.xls</h3>
				<table>
				<s:form action="file_upload" method="post"
					enctype="multipart/form-data" namespace='/file'>
					<s:file name="upload" label="上传的文件"></s:file>
					<s:submit cssClass="submit" value="上传"></s:submit>
					<s:hidden name="uploadPath" value="<span style="color:#ff0000;">files/salary</span>"></s:hidden>
				</s:form>
				</table>
			</div>
		</div>
	</div>
</body>
</html>

二、文件上传的action

代码语言: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 org.apache.struts2.ServletActionContext;

import edu.qdgxy.util.excel.ExcelDele;

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);
		excelDele.delFolder(path);//根据实际业务需求,执行清空文件夹
,		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();
		session.remove("uploadFileName");
		session.put("uploadFileName", uploadFileName);
		pages="excel_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;
	}
}	

三、structs.xml 配置action过滤器

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

四、文件删除

代码语言:javascript
复制
package edu.qdgxy.util.excel;

import java.io.File;

public class ExcelDele {
	public static void main(String args[]) {
		ExcelDele t = new ExcelDele();
		t.delFolder("E://test");
		System.out.println("deleted");
	}

	// 删除文件夹
	// param folderPath 文件夹完整绝对路径

	public void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			/*
			 * java.io.File myFilePath = new java.io.File(filePath);
			 * myFilePath.delete(); //删除空文件夹
			 */} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 删除指定文件夹下所有文件
	// param path 文件夹完整绝对路径
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				ExcelDele excelDele = new ExcelDele();
				excelDele.delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年05月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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