Struts中文件的上传与下载

前面学到的用组件去上传

前台:

  1、post表单提交

  2、表单类型 multipart/form-data

  3、intput type=file

后台:

  Apach提供的FileUpload组件

  核心类:

    FileItemFactory(FileItem的工厂)、SrvletFileUpload(Servlet中文上传的核心类)、FileItem(封装了上传的表单文件项信息)

其过程比较复杂

struts文件上传:

  文件上传拦截器帮助我们完成了问文件上传功能;

<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

同时还发现在默认执行的拦截器中有这个fileUpload,即默认这个拦截器会执行。

<interceptor-stack name="defaultStack">
    .....
    <interceptor-ref name="fileUpload"/>
    ........
</interceptor-stack>

文件上传案例:

首先为其配置环境:

<package name="uplaod_" namespace="/" extends="struts-default">
		<!-- 注意:action的名字 不能用关键字“fileUpload”,该名字以被使用过-->
		<action name="fileUploadAction" class="com.fileUpload_e.FileUpload">
			<result name="success">/e/success.jsp</result>
		</action>
</package>

同时在webRoot下建立一个upload文件夹,用于存放上传的文件

然后就是去写action来实现上传功能

在此之前,要先去看一下fileUpload的api,可以注意到该API的说明如下

  • [File Name] : File - the actual File
  • [File Name]ContentType : String - the content type of the file
  • [File Name]FileName : String - the actual name of the file uploaded (not the HTML name)

You can get access to these files by merely providing setters in your action that correspond to any of the three patterns above, such as setDocument(File document), setDocumentContentType(String contentType), etc.
See the example
code section

在其中也举了例子,如下:

public UploadAction extends ActionSupport {
       private File file;
       private String contentType;
       private String filename;

       public void setUpload(File file) {
          this.file = file;
       }

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

       public void setUploadFileName(String filename) {
          this.filename = filename;
       }

       public String execute() {
          //...
          return SUCCESS;
       }
  }

这个时候也可以参照这种写法:

package com.fileUpload_e;

import java.io.File;

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

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport{
	//对应表单中 的文件:<input type="file" name="file1">
	private File file1;
	//文件名(此格式要固定即name+FileName)
	private String file1FileName;
	//文件的类型(MIME)
	private String file1ContentType;
	public void setFile1(File file1) {
		this.file1 = file1;
	}
	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}
	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}

	@Override
	public String execute() throws Exception {
		/****拿到上传的文件 ,进行处理******/
		//把文件上传到upload目录
		//1、获取上传的目录的路径
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		//创建目标文件对象
		File destFile =new File(path,file1FileName);
		//2、把上传的文件拷贝到目标文件中
		FileUtils.copyFile(file1, destFile);
		return SUCCESS;
	}

}

提交页面如下:

	<form action="${pageContext.request.contextPath }/fileUploadAction" method="post" enctype="multipart/form-data">
  		用户名:<input type="text" name="userName"><br/>
  		文件:<input type="file" name="file1"><br/>

  		<input type="submit" value="上传">
  	</form>

就可以实现相关的功能了;

文件上传的先关细节:

  1、假设现在去上传一个大于2M的文件,就会出现以下错误

Struts中文件的上传与下载

可以看到这里的默认大小是2m。

(这里我遇到了一个问题是我的tomcat服务器如果遇到大于二十M的文件会因为版本的不同,在前台显示不同的错误,如果用tomcat7,会显示错误视图input,如果是tomcat8服务器会直接拒绝该请求,导致页面不能正常显示);

可以在default.properties中找到这个设置项

struts.multipart.maxSize=2097152

1、修改上传文件的大小。可以通过常量修改的方式来修改默认的其大小,如下

     <!-- 全局配置 -->
	<!-- 修改上传文件的大小为30M -->
	<constant name="struts.multipart.maxSize" value="31457280"></constant>

2、限制文件的运行的类型

我们发现不能再按照前面那种方式去限制文件类型了,因为在默认的struts的xml文件中,没有这个选项配置。

这个时候需要使用拦截器了:注入参数从而限制文件上传。如下

<package name="uplaod_" namespace="/" extends="struts-default">
		<!-- 注意:action的名字 不能用关键字“fileUpload”,该名字以被使用过-->
		<action name="fileUploadAction" class="com.fileUpload_e.FileUpload">
			<!-- 限制运行上传的文件类型 -->
			<interceptor-ref name="defaultStack">
				<!-- 限制的允许文件的扩展名(在fileUpload的拦截器中有allowedExtensions的设置) -->
				<param name="fileUpload.allowedExtensions">txt,jpg</param>
			</interceptor-ref>
			<result name="success">/e/success.jsp</result>
			<!-- 配置错误视图 -->
			<result name="input">/e/error.jsp</result>
		</action>

	</package>

也可以写如下的形式(一般不用)

<param name="fileUpload.allowedTypes">text/plain</param>

其中的error.jsp中可以显示错误原因

<body>
    This is Error page. <br>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <s:fielderror></s:fielderror>
  </body>

一旦上传非指定格式的文件会报以下的错误

Struts中文件的上传与下载

文件的下载

方式一:通过response对象想浏览器写入字节流数据,还要设置下载的响应头;(在struts中也可以,通过servletActionContext.getResponse()方法获得)

方式二:struts方式;

strut方式的文件下载如下:

首先要了解一下在struts的xml中用于文件流的下载的类,如下

<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

查找StreamResult相关的API,可以看到里面的example

<result name="success" type="stream">
   <param name="contentType">image/jpeg</param>
   <param name="inputName">imageStream</param>
   <param name="contentDisposition">attachment;filename="document.pdf"</param>
   <param name="bufferSize">1024</param>
 </result>

完整代码如下:

首先是其相关的Action类

package com.fileUpload_e;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 文件下载
 * 1、显示所有要下载的文件
 * 2、文件下载
 * @author Administrator
 *
 */
public class DownAction extends ActionSupport{

	/**************1、显示所有要下载的文件列表***********/
	public String list() throws Exception{
		//1、得到upload目录路径
		String path=ServletActionContext.getServletContext().getRealPath("/upload");
		//目录对象
		File file = new File(path);
		//2、得到所有要下的文件的文件名
		String[] fileNames=file.list();
		//保存到域对象
		ActionContext ac=ActionContext.getContext();
		//得到代表request的map
		Map<String, Object> request=(Map<String, Object>) ac.get("request");
		request.put("fileNames", fileNames);
		return "list";
	}

	/**************2、文件下载*********************/
	private String fileName;
	public void setFileName(String fileName) {
		try {
			//处理传入的参数乱码问题(get方式提交)
			fileName=new String(fileName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		//处理好的文件名赋值
		this.fileName = fileName;
	}
	//1、下载提交的业务逻辑(在struts.xml中配置返回stream)
	public String download() throws Exception{

		return "download";
	}
	//2、返回文件流的方法
	public InputStream getAttrInputStream(){
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);
	}
	//3、下载显示文件名(浏览器显示的文件名)
	public String getDownFileName(){
		try {
			//浏览器显示需要中文编码
			fileName=URLEncoder.encode(fileName,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileName;
	}
}

配置文件

<action name="down_*" class="com.fileUpload_e.DownAction" method="{1}">
			<!-- 列表展示 -->
			<result name="list">/e/list.jsp</result>
			<!-- 下载操作 -->
			<result name="download" type="stream">
				<!-- 调用action中返回的方法 -->
				<!-- 运行下载的文件的类型:指定为所有二进制文件类型 -->
			   <param name="contentType">application/octet-stream</param>
			   <!-- 对应的Action中的属性:返回流的属性【即在DownAction中的getAttrInputStream()】 -->
			   <param name="inputName">AttrInputStream</param>
			   <!-- 浏览器显示的文件名(下载头) -->
			   <param name="contentDisposition">attachment;filename="${downFileName}"</param>
			   <!-- 缓冲区的大小 -->
			   <param name="bufferSize">1024</param>
			</result>
</action>

下载页:

<%@ 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>下载列表</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>
    <table border="1" align="center">
    	<tr>
    		<td>编号</td>
    		<td>文件名</td>
    		<td>操作</td>
    	</tr>
    	<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    	<c:forEach var="fileName" items="${fileNames }" varStatus="vs">
			<tr>
				<td>${vs.count }</td>
				<td>${fileName }</td>
				<td>
					<!-- 构建一个url -->
					<c:url var="url" value="down_download">
						<c:param name="fileName" value="${fileName }"></c:param>
					</c:url>
					<a href="${url }">下载</a>
				</td>

			</tr>
    	</c:forEach>
    </table>
  </body>
</html>

js小练习:评价图标的使用:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>评价</title>
    <style type="text/css" media="screen">
    * {
        margin: 0;
        padding: 0;
    }

    li {
        width: 30px;
        height: 30px;
        background: url(star.jpg) no-repeat 0 -70px;
        float: left;
        list-style: none;
    }
    </style>
</head>

<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <script type="text/javascript">
    var aLi = document.getElementsByTagName('li');
    var bBtn = true;

    for (var i = 0; i < aLi.length; i++) {
        aLi[i].index = i;
        aLi[i].onmouseover = function() {
            bBtn = true;
            if (!bBtn) {
                return;
            }
            for (var i = 0; i < aLi.length; i++) {
                if (i <= this.index) {
                    aLi[i].style.backgroundPosition = '0 0';
                } else {
                    aLi[i].style.backgroundPosition = '0 -70px'
                }
            }
        }

        aLi[i].onmouseout = function() {
            if (!bBtn) {
                return;
            }
            for (var i = 0; i < aLi.length; i++) {
                aLi[i].style.backgroundPosition = '0 -70px';
            }
        }

        aLi[i].onclick = function() {
            bBtn = false;
        }

    }
    </script>
</body>

</html>

如图:
Struts中文件的上传与下载

上一篇:java批量插入数据进数据库中


下一篇:无线网卡连接网络后共享给本地有线网卡使用(Win10)