一、文件上传
/**
* 上传供导入的数据文件
*
* @return
* @throws IOException
*/
public String uploadForImport() throws IOException {
File impdir = new File(datadir, "imp");
if (!impdir.exists()) {
impdir.mkdirs();
}
File newFile = new File(impdir, uploadFileName);
FileCopyUtils.copy(upload, newFile);
newFileInfo = new FileInfo(newFile);
return SUCCESS;
}
其中datadir字符串是服务器上存储上传下载的文件夹的(当datadir=”D:/var”,文件就会上传到服务器的D:\var\imp文件夹中)。
在struts2中的上传配置:
<package name="json-custom" extends="json-default">
<action name="uploadForImport" class="net.zkbc.p2p.mgt.action.FileAction" method="uploadForImport">
<result type="json">
<param name="root">newFileInfo</param>
</result>
</action>
</package>
其中datadir是通过spring的@Value注解注入的:
(import org.springframework.beans.factory.annotation.Value;)
//job.datadir:后面的是默认值
@Value("${job.datadir:/var/zkbc/p2p-por/data}")
private String datadir;
注:注解在spring中的都配置:
<!-- 开启注解 -->
<context:annotation-config />
<!-- spring注解的包,也就是写有@Value注解类的包 -->
<context:component-scan base-package="net.zkbc.p2p.mgt.action"/>
<!-- 引入.properties文件 -->
<context:property-placeholderlocation="classpath:net/zkbc/p2p/mgt/action/job.properties" />
在job.properties中有:
job.datadir=D:/var
二、文件下载:
/**
* @return 供下载的导出的文件的输入流
* @throws FileNotFoundException
*/
public InputStream getInputStreamForExport() throws FileNotFoundException {
return new FileInputStream(new File(datadir, "exp/" + downloadFileName));
}
将在datadir文件夹下的exp文件夹中的莫个文件的文件输入流返回提供文件下载的输入流。
在struts2中关于下载文件的配置:
<action name="*DownloadForExport" class="net.zkbc.p2p.mgt.action.FileAction">
<param name="downloadFileName">{1}.txt</param>
<!-- 配置结果类型 为stream-->
<result type="stream">
<param name="contentType">application/octet-stream</param>
<!-- 通过getInputStreamForExport()返回下载文件的InputStream -->
<param name="inputName">inputStreamForExport</param>
<param name="contentDisposition">attachment;filename="{1}.txt"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
*DownloadForExport 中的 * 表示以任意字符开头DownloadForExport.action后面的文件名downloadFileName属性中的{1}就表示 * 所代表的内容
如:testDownloadForExport
那么下载的的文件名就是: test.txt
Web.xml中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring配置的文件 --><param-value>/WEB-INF/conf/structs/beans.xml</param-value>
</context-param>
<listener>
<!-- 监听spring配置文件的监听器 -->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<!-- struts2中用到的配置,在struts-default.xml文件中有struts-default包,在struts-plugin.xml文件中有josn-default包,../conf/structs/struts.xml,../conf/structs/custom.xml是自己的的struts配置 --><param-value>struts-default.xml,struts-plugin.xml,../conf/structs/struts.xml,../conf/structs/custom.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在jsp中的代码:
upload.jsp:
<%@ page contentType="text/html;charset=UTF-8" %>
<!-- 引入jstl标签 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 将工程应用名动态绑定到jsp的pageContext对象当中,后面就可以用EL表达式获取工程应用名了,这样就不用
把应用名在路径中写死了,当在容器中部署工程改变应用名后,不用再到jsp中修改代码了。
-->
<%pageContext.setAttribute("base", request.getContextPath());%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8">
<title>上传文件</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ${base}:EL的使用,动态获取工程应用名 -->
<link rel="stylesheet" href="${base}/js/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="${base}/js/jquery.fileupload/8.8.5/css/jquery.fileupload-ui.css">
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="tabbable" id="tabs">
<ul class="nav nav-tabs">
<li class="active">
<a href="#panel-imp" data-toggle="tab">未导入</a>
</li>
<li>
<a href="#panel-done" data-toggle="tab">已导入</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="panel-imp">
<p>
<div id="fileupload">
<span class="btn btn-info fileinput-button" type="button">
<span>上传</span><input type="file" name="upload" />
</span>
<span class="fileupload-loading"></span>
</div>
<table class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>名称</th>
<th>大小</th>
<th>时间</th>
</tr>
</thead>
<tbody>
<!-- 使用jstl中的forEach标签,其中items放的是由EL表达式从Action中获取到的impFileInfoList集合,即迭代的对象;
var中放的是每次从impFileInfoList集合中取出的单个对象,是每个对象的名字;
varStatus是迭代变量的名称,用来表示迭代的状态,
可以访问到迭代自身的信息 -->
<c:forEach items="${impFileInfoList}" var="r" varStatus="status">
<tr>
<!-- ${status.index + 1}表示迭代到第几个对象了 -->
<td>${status.index + 1}</td>
<!-- ${r.name}:通过EL表达式获取迭代的当前对象的name属性 -->
<td>${r.name}</td>
<td>${r.size}</td>
<td>${r.lastModified}</td>
</tr>
</c:forEach>
</tbody>
</table>
</p>
</div>
<div class="tab-pane" id="panel-done">
<p>
<table class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>名称</th>
<th>大小</th>
<th>时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${doneFileInfoList}" var="r" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${r.name}</td>
<td>${r.size}</td>
<td>${r.lastModified}</td>
</tr>
</c:forEach>
</tbody>
</table>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="${base}/js/jquery/1.10.2/jquery.min.js"></script>
<script src="${base}/js/jquery.fileupload/8.8.5/js/vendor/jquery.ui.widget.js"></script>
<script src="${base}/js/jquery.fileupload/8.8.5/js/jquery.iframe-transport.js"></script>
<script src="${base}/js/jquery.fileupload/8.8.5/js/jquery.fileupload.js"></script>
<script src="${base}/js/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="${base}/js/handlebars/1.1.2/handlebars.js"></script>
<script>
$(function () {
'use strict';
var template = Handlebars.compile($('#row-template').html());
<!-- jquery中的文件上传方法 -->
$('#fileupload').fileupload({
<!-- 请求的路径 -->
url: '${base}/uploadForImport.action',
<!-- 返回数据类型为json类型 -->
dataType: 'json',
start: function(e) {
$('#fileupload').addClass('fileupload-processing');
},
done: function (e, data) {
$('#fileupload').removeClass('fileupload-processing');
var tbody = $('#panel-imp tbody');
data.result.lineNo = tbody.find('tr').size() + 1;
$(template(data.result)).appendTo(tbody);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>
<script id="row-template" type="text/x-handlebars-template">
<tr>
<td>{{lineNo}}</td>
<td>{{name}}</td>
<td>{{size}}</td>
<td>{{lastModified}}</td>
</tr>
</script>
</body>
</html>
应用工厂目录:
jar包: