struts2 文件上传

一、文件上传页面

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

<%@ 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

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过滤器

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

四、文件删除

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



 


 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值