JS+Struts2多文件上传实例详解
作者:袭烽 发布时间:2022-02-18 21:08:02
标签:js,Struts2,文件上传
本文实例为大家分享了JS Struts2多文件上传的具体代码,供大家参考,具体内容如下
1、JSP页面:
JS控制增加删除多个上传文件框,代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@include file="../../_head.html"%>
<title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script language="javascript" type="text/javascript"
src="../js/common/common.js"></script>
<script type="text/javascript">
var pos = 1;
function addFileComponent() {
var elTable = document.getElementById('uploadTable').getElementsByTagName('tbody')[0];
var elTr = document.getElementById('fileTr');
var elTr2 = document.getElementById('op');
var newEleTr = elTr.cloneNode(true);
newEleTr.id = "fileTr" + pos;
newEleTr.style.display = "";
inputs = newEleTr.getElementsByTagName('input');
inputs[0].id="file" + pos;
var elInput = inputs[1];
elInput.onclick=delFileComponent;
elInput.id="delbutton" + pos++;
elTable.insertBefore(newEleTr, elTr2);
}
function delFileComponent() {
var elTable = document.getElementById('uploadTable').getElementsByTagName('tbody')[0];
var trArr = elTable.getElementsByTagName("tr");
var el = event.srcElement;
for(j = 0; j < trArr.length; j++) {
tr = trArr[j];
if(tr.getElementsByTagName("input")[1] == el) {
elTable.removeChild(tr);
pos--;
break;
}
}
}
function isValidateFile(obj){
var extend = obj.value.substring(obj.value.lastIndexOf(".")+1);
if(extend==""){
}else{
if(!(extend=="xls"||extend=="doc")){
alert("请上传后缀名为xls或doc的文件!");
var nf = obj.cloneNode(true);
nf.value='';
obj.parentNode.replaceChild(nf, obj);
return false;
}
}
return true;
}
</script>
</head>
<body>
<%@ include file="/common/message.jsp"%>
<div class="body-box">
<div class="rhead">
<div class="rpos">
文件上传(可同时上传多份文件)
</div>
<div class="clear"></div>
</div>
<s:form id="ops" action="csc_mUploadFile" theme="simple"
cssClass="rhead" enctype = "multipart/form-data">
<table id="uploadTable" width="100%" border="0">
<tr>
<td>
<input type="file" id="file0" name="uploadFile" size="50"
onchange="isValidateFile(this);" />
</td>
</tr>
<tr id="fileTr" style="display: none;">
<td>
<input type="file" size="50" name="uploadFile"
onchange="isValidateFile(this);" />
<input type="button" value="删除" />
</td>
</tr>
<tr id="op">
<td>
<input type="submit" id="uploadbutton" value="上传" />
<input type="button" value="添加" id="addbutton"
onClick="addFileComponent();" />
</td>
</tr>
</table>
</s:form>
<table class="pn-ltable" width="100%" cellspacing="1" cellpadding="0"
border="0">
<thead class="pn-lthead">
<tr>
<th>
序号
</th>
<th>
文件名
</th>
<th>
上传时间
</th>
</tr>
</thead>
<tbody class="pn-ltbody">
<tr onmouseover="Pn.LTable.lineOver(this);"
onmouseout="Pn.LTable.lineOut(this);"
onclick="Pn.LTable.lineSelect(this);">
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
2、Action后台处理上传文件:
//uploadFile对应页面<input type="file" name="uploadFile">
private List<File> uploadFile;
//文件名对应uploadFile+“FileName”,要不获取不到文件名
private List<String> uploadFileFileName;
// 文件上传
public String mUploadFile() {
if (null == uploadFile) {
this.addActionError("请上传文件!");
} else {
String fileName = "";
try {
//在自己代码中控制文件上传的服务器目录
String directory = ServletActionContext.getServletContext().getRealPath("/uploads");
//判断该目录是否存在,不存在则创建
FileUtil.makeDir(directory);
//循环处理上传的文件
for(int i=0,j=uploadFile.size();i<j;i++){
fileName = uploadFileFileName.get(i);
String filePath = directory + File.separator + fileName;
FileUtil.uploadFile(uploadFile.get(i), new File(filePath));
}
} catch (IOException e) {
this.addActionMessage("");
}
this.addActionMessage("文件上传成功!");
}
return "fileUpload";
}
FileUtil代码如下:
public class FileUtil {
private static final int BUFFER_SIZE = 16 * 1024;
public static void uploadFile(File src, File dst) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
}
public static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public static void makeDir(String directory) {
File dir = new File(directory);
if (!dir.isDirectory()) {
dir.mkdirs();
}
}
public static String generateFileName(String fileName)
throws UnsupportedEncodingException {
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());
String extension = fileName.substring(fileName.lastIndexOf("."));
fileName = new String(fileName.getBytes("iso8859-1"), "gb2312");
return fileName + "_" + formatDate + new Random().nextInt(10000)
+ extension;
}
}
扩展:
1.可以实现带进度条的上传与下载;
2.可以用xml文件记录上传的文件清单,并且可以根据页面对上传文件的操作来修改相应的xml文件;
完毕!
来源:https://blog.csdn.net/shimiso/article/details/6045169
0
投稿
猜你喜欢
- 一:问题描述 在已经root过的android设备下,app执行一个linux命令,app需要获取su权限,在某些a
- 闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能。于是乎开始学习.Net Remoting..Net Remoting 是由客户端通过R
- 将JavaDoc 注释 生成API文档1. 打开java代码,编写JavaDoc 注释,只有按照java的规范编写注释,才能很好的生成API
- 本文实例讲述了Android编程设计模式之迭代器模式。分享给大家供大家参考,具体如下:一、介绍迭代器模式(Iterator Pattern)
- 本文实例讲述了Java基于Tcp的基础聊天功能。分享给大家供大家参考,具体如下:最基础的聊天,用户端和服务器端每次说一句,而且严格规定了先后
- Java中的final关键字1、修饰类的成员变量 这是final的主要用途之一,和C/C++的const,即该成员被修饰为常量,意味着不可修
- 前言最近在开发项目的时候涉及到复杂的动态条件查询,但是mybaits本身不支持if elseif类似的判断但是我们可以间接通过 chose
- application.properties有以下这几条数据方法一:@Value注解+@Component建议properties少的时候用
- 前言在 Java 中,跳转的实现方式有两种:请求转发和请求重定向,但二者是完全不同的,所以我们今天就来盘它。请求转发和请求重定向主要区别,包
- 0.前言文章需求:对于学生来说,目前网上确实没有比较统一而且质量好的支付教程。因为支付对个人开发者尤其是学生来说不太友好。因此,自己折腾两天
- 作为代码质量检查的流行工具,比如Sonarqube能够检查代码的“ * ”,跟代码结合起来能够更好地提高代码的质量,这篇文章将会介绍如何结合
- 本文实例讲述了C#静态static的用法,分享给大家供大家参考。具体用法分析如下:一、静态类静态类与非静态类的重要区别在于静态类不能实例化,
- 本文实例为大家分享了RecylerView实现流布局的具体代码,供大家参考,具体内容如下第一步:添加依赖compile 'com.a
- Android webview旋转屏幕导致页面重新加载问题解决办法1. 在create时候加个状态判断protected void onCr
- 一.继承的类型在面向对象的编程中,有两种截然不同继承类型:实现继承和接口继承1.实现继承和接口继承*实现继承:表示一个类型派生于基类型,它拥
- 一、获取接口请求的数据可以在Interceptor的afterCompletion中实现但是要重写RequestWrapper代码记录如下:
- 项目中常用到的圆形进度条有好多个,从网上搜到的自定义进度条多是封装的比较好的代码,但是不利于初学者,现在本博客就教给大家如何一步步实现自定义
- 前言Spring 5发布有两年了,随Spring 5一起发布了一个和Spring WebMvc同级的Spring WebFlux。这是一个支
- 本人是从事互联网金融行业的,所以会接触到一些金融类的问题,常见的一种就是数字转汉字大小写的问题。所以抽空就写了一个小小的工具类,实现了数字转
- 一、问题场景使用Logger.error方法时只能打印出异常类型,无法打印出详细的堆栈信息,使得定位问题变得困难和不方便。二、先放出结论Lo