`

Struts2上传下载

阅读更多
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>

<constant name="struts.multipart.maxSize" value="4194304"></constant>
	<package name="upLoad" namespace="/upLoad" extends="struts-default">
		<action name="*-*" class="com.wepull.action.{1}Action" method="{2}">
			<interceptor-ref name="fileUpload">
				   <param name="maximumSize">204800</param><!-- 单个上传文件不能超过200K   -->
				   <param name="allowedTypes">image/jpeg,image/gif</param>  <!-- 允许上传的类型 --> 
			</interceptor-ref>
			<interceptor-ref name="defaultStack" />
			<result name="success">/success.jsp</result>
			<result name="input">/failure.jsp</result><!-- 上传失败返回input -->
		</action>
	</package>
	
	<package name="down" namespace="/down" extends="struts-default">
		<action name="*-*" class="com.wepull.action.{1}Action" method="{2}">
			<param name="path">down/王.jpg</param>
			<result name="down-success" type="stream">
				<param name="contentType">image/jpeg,image/gif</param>
				<param name="inputName">inputStream</param>
				<param name="contentDisposition">attachment;filename=${fileName}</param>
				<param name="bufferSize">4096</param>
				
			</result>
		</action>
	</package>
</struts>



 下载java代码:

package com.wepull.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport {

	private String fileName;
	private String path;

	public String down() {
		fileName = path.substring(path.lastIndexOf("/") + 1);// 截取文件名字
		// fileName= "王.jpg";
		System.out.println(fileName);
		return "down-success";

	}

	public String getFileName() {
		// 转换成西欧字符集
		try {
			fileName = new String(fileName.getBytes(), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

	public InputStream getInputStream() {
		InputStream is = ServletActionContext.getServletContext()
				.getResourceAsStream(path);
		return is;
	}

	public void setPath(String path) {
		this.path = path;
	}
}

下载前台页面:

<%@ 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 'index.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="down/Down-down">点击下载玉照</a> <br>
  </body>
</html>



  

上传代码:上传单个文件

package com.wepull.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UpLoadAction extends ActionSupport {

	private File fileUp;//一定要和form表单中 上传框的name的属性值保持一致
	private String fileUpFileName;// 上传文件的名字 		 必须以FileName结尾
	private String fileUpContentType;//上传文件的名字类型   必须以ContentType结尾
	
	public String upLoad(){
		String path = ServletActionContext.getServletContext().getRealPath("/");
		System.out.println("发布工程的路径:"+path);//D:\Program Files\apache-tomcat-6.0.24\webapps\1102_Struts_upLoad
		File saveDir = new File(path+File.separator+"image");
//		File saveDir = new File(path,"image");
		if(!saveDir.exists())saveDir.mkdirs();
		
		File saveFile = new File(saveDir,fileUpFileName);//要保存的文件
		
		try {
			FileUtils.copyFile(fileUp,saveFile );//拷贝文件
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
		
	}

	public File getFileUp() {
		return fileUp;
	}

	public void setFileUp(File fileUp) {
		this.fileUp = fileUp;
	}

	public String getFileUpFileName() {
		return fileUpFileName;
	}

	public void setFileUpFileName(String fileUpFileName) {
		this.fileUpFileName = fileUpFileName;
	}

	public String getFileUpContentType() {
		return fileUpContentType;
	}

	public void setFileUpContentType(String fileUpContentType) {
		this.fileUpContentType = fileUpContentType;
	}
}

  

上传代码:上传单个文件 对应前台代码

<%@ 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 'index.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>
    <form action="upLoad/UpLoad-upLoad" enctype="multipart/form-data" method="post">
    	<input type="file" name="fileUp"><br>
    	<input type="submit" value="上传"> 
    	<input type="reset" value="重置"> 
    
    </form>
  </body>
</html>





 

 

上传代码2:上传多个文件

package com.wepull.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UpLoad2Action extends ActionSupport {

	private File[] fileUp;//一定要和form表单中 上传框的name的属性值保持一致
	private String[] fileUpFileName;// 上传文件的名字 		 必须以FileName结尾
	private String[] fileUpContentType;//上传文件的名字类型   必须以ContentType结尾
	
	public String upLoad(){
		String path = ServletActionContext.getServletContext().getRealPath("/");
		System.out.println("发布工程的路径:"+path);//D:\Program Files\apache-tomcat-6.0.24\webapps\1102_Struts_upLoad
		File saveDir = new File(path+File.separator+"image");
//		File saveDir = new File(path,"image");
		if(!saveDir.exists())saveDir.mkdirs();
		
		for (int i = 0; i < fileUp.length; i++) {
			File saveFile = new File(saveDir,fileUpFileName[i]);//要保存的文件
			try {
				FileUtils.copyFile(fileUp[i],saveFile );//拷贝文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		return SUCCESS;
		
	}

	public File[] getFileUp() {
		return fileUp;
	}

	public void setFileUp(File[] fileUp) {
		this.fileUp = fileUp;
	}

	public String[] getFileUpFileName() {
		return fileUpFileName;
	}

	public void setFileUpFileName(String[] fileUpFileName) {
		this.fileUpFileName = fileUpFileName;
	}

	public String[] getFileUpContentType() {
		return fileUpContentType;
	}

	public void setFileUpContentType(String[] fileUpContentType) {
		this.fileUpContentType = fileUpContentType;
	}

	
}

 

上传代码2:上传多个文件 对应前台代码

<%@ 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 'index.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>
    <form action="upLoad/UpLoad2-upLoad" enctype="multipart/form-data" method="post">
    	<input type="file" name="fileUp"><br>
    	<input type="file" name="fileUp"><br>
    	<input type="file" name="fileUp"><br>
    	<input type="submit" value="上传"> 
    	<input type="reset" value="重置"> 
    
    </form>
  </body>
</html>





 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics