简单使用Struts2 实现上传下载功能

1.首先创建一个web项目

2.使用myeclipse自带的功能配置成struts项目

代码开始

首先是下载页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'download.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <a href="downloadAction?fileName=F:/TEMP/AJAX.txt">下载文件</a> <br>
  </body>
</html>
上传页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>My JSP 'UpLoad.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <br>
      <s:form action="uploadAcion" method="post" enctype="multipart/form-data">
      	<s:file name="file" label="请选择要上传的问题件"></s:file>
      	<s:submit></s:submit>
      </s:form>
  </body>
</html>

下载的Action

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
	private String fileName;
	//固定参数名,用于得到文件内容类型
	private String contentType;
	//输入流,输入到内存
	private InputStream inputStream;
	//将页面上要下载的文件类型通过后缀名转换为服务器能识别的文件类型
	static Map<String,String> contentTypes = new HashMap<String,String>();
	
	static{
		contentTypes.put("doc", "application/msword");
		contentTypes.put("ppt", "application/vnd.ms-powerpoint");
		contentTypes.put("xsl", "application/vnd.ms-excel");
		contentTypes.put("pdf", "application/pdf");
		contentTypes.put("html", "text/html");
		contentTypes.put("htm", "application/html");
		contentTypes.put("gif","image/gif");
		contentTypes.put("jpeg","image/jpeg");
		contentTypes.put("mpeg","video/mpeg");
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getContentType(String fileName) {
		for(String ext:contentTypes.keySet()){
			if(fileName.toLowerCase().endsWith(ext)){
				return contentTypes.get(ext);
			}
		}
		//代表二进制传输文件
		return "application/octet-stream";
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	
	@Override
	public String execute() {
		File file = new File(fileName);
		try{
			inputStream = new FileInputStream(file);
		}catch(Exception e){
			e.printStackTrace();
		}
		fileName = file.getName();
		contentType = this.getContentType(fileName);
		return SUCCESS;
	}


}

上传的action

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	//file控件的名字
	private String file;
	//文件类型,后缀是固定的
	private String fileContentType;
	//文件的名字
	private String fileFileName;
	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	@Override
	public String execute(){
		try {
			InputStream is = new FileInputStream(file);
			//一次传输的大小
			byte[] b = new byte[1024];
			int length = is.read(b);
			
			//目标文件夹
			String path ="F:/TEMP";
			File f = new File(path);
			//如果不存在就创建
			if(!f.exists()){
				f.mkdir();
			}
			//输出流,从内存中输出到内存外
			OutputStream os = new FileOutputStream(new File(path+"/"+fileFileName));
			while(length>0){
				os.write(b);
				length = is.read();
			}
			is.close();
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	
	}
}


struts.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="pk" extends="struts-default" namespace="/">
		<action name="uploadAcion" class="action.UploadAction">
			<result>/upload.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>
		
		<action name="downloadAction" class="action.DownloadAction">
			<result type="stream">
				<param name="contentType">${contentType}</param>
				<param name="contentDisposition">attachment;filename=${fileName }</param>
				<param name="inputName">inputStream</param>
				<param name="bufferSize">1024</param>
			</result>
		
		</action>
	</package>
</struts>    

配置struts.properties 代码

struts.multipart.parser=jakarta
struts.multipart.saveDir=C\:/TEMP
struts.multipart.maxSize=102400



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值