`
sha1064616837
  • 浏览: 39913 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

struts2上传与下载

阅读更多
1.action代码:
/**
 * 文件处理的底层action 供需要处理文件的action继承
 * @author xxx
 * @date 2014-10-14
 * @time 下午3:24:33
 * @version 1.0
 */
@SuppressWarnings("serial")
public abstract class FileAction extends AbstractAction{

	private static Logger logger = Logger.getLogger(FileAction.class);
	/** 文件名*/
	public String uploadFileName;
	/** 上传文件属性,与页面中type=file中定义name的保持一致*/
	private File upload;
	/**导入文件的保存目录*/
	private String savePath="/upload"+File.separator+DateUtil.getSysDateStr();
	/**导入模板的保存目录*/
	private String modelPath="/model";
	/**文件最终保存路径*/
	private String targetPath;
	/**文件下载路径*/
	private String filePath;
	
	/**
	 * 上传文件
	 */
	public String uploadFile() 
	{
		FileInputStream in = null;
		FileOutputStream out = null;
		if(Detect.notEmpty(uploadFileName))
		{
			String directoryName = getSavePath();
			File dir = new File(directoryName);
			if (!dir.exists())
			{
				boolean flag = dir.mkdir();
				if(!flag)
				{
					logger.error("文件保存路径"+directoryName+"不存在且自动创建失败,请先手工创建");
					return ERROR;
				}
			}
			targetPath = dir.getPath() + File.separator + uploadFileName;
			File uploadDestination = new File(targetPath);
			try {
				in = new FileInputStream(upload);
				out = new FileOutputStream(uploadDestination);
				int c;
				while ((c = in.read()) != -1) {
					out.write(c);
				}
			}catch (IOException e) {
				e.printStackTrace();
				logger.error("上传文件失败"+e.getMessage());
				return ERROR;
			}
			finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
						logger.error("关闭Input流失败"+e.getMessage());
						return WebConstant.ERROR;
					}finally{
						in = null;
					}
				}
				if (out != null) {
					try {
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
						logger.error("关闭Output流失败"+e.getMessage());
						return ERROR;
					}finally{
						out = null;
					}
				}
			}
		}
		else
		{
			logger.error("请选择上传的文件");
			return INPUT;
		}
		logger.info("文件上传成功,保存路径为:"+targetPath);
		return SUCCESS;
	}
	
	/**
	 * 下载文件
	 */
	public String downloadFile()
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		if(Detect.notEmpty(filePath))
		{
			boolean result = exportFile(response, filePath);
			if(result){
				logger.info("下载成功");
				return SUCCESS;
			}else
			{
				logger.warn("下载失败");
				return ERROR;
			}
		}else{
			return ERROR;
		}
	}

	/**
	 * 文件导出
	 * @param response
	 * @param filePath
	 * @return
	 */
	private boolean exportFile(HttpServletResponse response,String filePath) 
	{
		DataInputStream dis = null;
		OutputStream toClient = null;
		try
		{
			File file = new File(filePath); // 取得要传送的文件
			String fileName = file.getName();
			dis = new DataInputStream(new FileInputStream(file));
			byte[] b = new byte[1024];
			long totalsize = 0;
			long filelength = file.length(); // 文件长度
			response.reset();// 清空response
			// 设置响应头信息,让下载的文件显示保存信息
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ new String(fileName.getBytes("GBK"), "ISO8859_1"));
			response.addHeader("Content-length", "" + filelength);
			toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			// 发送文件数据,每次1024字节,最后一次单独计算
			while (totalsize < filelength){
				totalsize += 1024;
				if (totalsize > filelength)
				{ // 最后一次传送的字节数
					byte[] leftpart = new byte[1024 - (int)(totalsize - filelength)];
					dis.readFully(leftpart);
					toClient.write(leftpart);
				} else{
					dis.readFully(b); // 读1024个字节至字节数组b
					toClient.write(b);
				}
			}
			toClient.flush();
			toClient.close();
			dis.close();
		} catch (Exception e){
			e.printStackTrace();
			return false;
		} finally{
			if (null != toClient){
				try{
					toClient.close();
					toClient = null;
				} catch (IOException e){
					e.printStackTrace();
				}
			}
			if (null != dis){
				try{
					dis.close();
					dis = null;
				} catch (IOException e){
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(this.savePath);
	}
	public String getModelPath()
	{
		return ServletActionContext.getServletContext().getRealPath(this.modelPath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public void setModelPath(String modelPath)
	{
		this.modelPath = modelPath;
	}
	public String getTargetPath() {
		targetPath=org.apache.commons.lang.StringUtils.replaceChars(targetPath, '\\', '/');
		return targetPath;
	}
	public void setTargetPath(String targetPath) {
		this.targetPath = targetPath;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getFilePath()
	{
		return filePath;
	}
	public void setFilePath(String filePath)
	{
		this.filePath = filePath;
	}

jsp页面:上传和下载都需要from表单提
上传:
<form action="" method="post" enctype="multipart/form-data" id="csvform">文件:
<input type='file' onchange="xxx()" id="upload" name="upload" >
<a class="easyui-linkbutton" href="javascript:importFile()" icon="icon-import" style="margin:10px 25px;">导入数据</a>
<a class="easyui-linkbutton" href="javascript:clearFile()" icon="icon-checkbox" >清除文件</a>
</form>
$('#csvform').attr("action", actionName);//动态给form
分享到:
评论

相关推荐

    struts2文件上传下载源代码

    在Struts2中,文件上传和下载是常见的功能需求,特别是在处理用户交互和数据交换时。这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件...

    struts2实现文件上传下载

    本篇文章将详细探讨如何在Struts2框架下实现文件的上传与下载。 首先,我们需要了解Struts2中的文件上传机制。Struts2提供了`FileUploadInterceptor`拦截器来处理文件上传请求。在处理文件上传时,开发者需要在...

    struts2上传和下载文件详细源码

    在这个"struts2上传和下载文件详细源码"中,我们可以深入理解Struts2如何处理文件上传和下载操作。 1. 文件上传: 在Struts2中,文件上传主要依赖于Apache的Commons FileUpload库。首先,需要在struts.xml配置文件...

    struts2上传下载项目

    在"struts2上传下载项目"中,我们可以深入理解如何利用Struts2实现文件的上传与下载功能。 首先,上传功能在Web应用中十分常见,比如用户在注册时上传头像,或者提交文档等。在Struts2中,我们主要借助`struts2-...

    Struts2 上传和下载功能

    在Struts2中,实现文件上传和下载是常见的需求,对于用户交互和数据交换至关重要。这篇博客文章可能详细讨论了如何在Struts2框架中实现这两个功能。 在Struts2中,文件上传主要依赖于`Commons FileUpload`库,这是...

    Struts2文件上传与下载

    Struts2是一个强大的Java web框架,它为开发者提供了丰富的功能,包括处理用户表单提交、进行文件上传和下载。在Web应用中,文件上传和下载是常见的需求,例如用户上传头像、下载文档等。Struts2通过其Action类和...

    struts2文件上传与下载

    Struts2是一个非常流行的...以上就是Struts2中文件上传与下载的基本实现,通过这些步骤,你可以实现用户在Web应用中上传和下载文件的功能。在实际项目中,还需要根据具体需求进行调整和优化,如错误处理、进度显示等。

    struts2上传下载实例

    在Struts2中,文件上传和下载是常见的功能需求,尤其对于处理用户提交的表单数据时,如上传图片、文档等。这个"struts2_上传下载"实例则涵盖了多种实现这些功能的方法。 首先,Struts2的文件上传依赖于Apache的...

    struts2文件上传下载

    在这个特定的项目中,我们关注的是"struts2文件上传下载"的功能,这涉及到用户通过Web界面上传文件到服务器,以及从服务器下载文件到用户的设备。 文件上传是Web应用中的常见需求,例如用户可能需要提交图片、文档...

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts上传与下载

    以上就是关于Struts文件上传与下载的核心知识点。实际开发中,还需要结合具体的业务需求和安全性要求进行优化和调整。理解这些概念并熟练运用,你就能在Struts框架下实现高效且安全的文件上传和下载功能。

    struts2文件上传和下载

    在Struts2中,文件上传和下载是常见的功能需求,对于用户交互和数据交换至关重要。以下是对这些知识点的详细阐述: 1. **文件上传**: 在Struts2中,文件上传主要依赖于`Commons FileUpload`库,它是一个Apache提供...

    struts2图片上传并预览

    Struts2提供了一套完善的机制来处理文件上传,包括图片。本文将详细讲解如何利用Struts2实现图片上传并进行预览。 一、Struts2文件上传基础 1. 添加依赖:在项目中,你需要添加Struts2的核心库和文件上传插件。...

    struts与hibernate实现文件的上传与动态下载

    通过以上步骤,你可以实现一个基于Struts2和Hibernate的文件上传与动态下载系统。这个系统能够处理用户上传的文件,将其保存到服务器,同时提供动态下载功能,允许用户根据需要下载文件。在实际开发中,还需要考虑...

    基于Struts2的文件上传下载功能的完整源代码。

    在基于Struts2的文件上传下载功能中,它提供了处理用户上传文件和提供文件下载的服务。这个完整的源代码是实现这些功能的一个实例,经过测试确保了其正确性和可用性。 首先,我们要理解Struts2中的Action类。Action...

    struts图片上传与下载

    本文将深入探讨Struts中图片的上传与下载流程,并结合SQL数据库的使用,为开发者提供实用的指导。 **1. 图片上传** 图片上传在Web应用中主要涉及以下步骤: 1. **前端页面设计**:创建一个HTML表单,包含`...

    struts2上传下载功能(基于MyEclipse平台)

    本项目基于MyEclipse平台,实现了Struts2框架下的文件上传与下载功能,这是Web应用中常见的需求,例如用户可以上传头像、文档等,或者下载公共资源。 在Struts2中,文件上传主要依赖于`struts2-core`库中的`...

    struts2真正实现上传下载完整源代码

    总结来说,Struts2通过与Commons FileUpload库的集成,提供了便捷的文件上传和下载功能。通过Action和配置文件的设置,我们可以轻松地处理用户上传的文件,并实现文件的下载服务。在实际项目中,还需要考虑错误处理...

Global site tag (gtag.js) - Google Analytics