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后台处理上传文件:
-
privateList<File>uploadFile;
-
-
privateList<String>uploadFileFileName;
-
-
publicStringmUploadFile(){
-
if(null==uploadFile){
-
this.addActionError("请上传文件!");
-
}else{
-
StringfileName="";
-
try{
-
-
Stringdirectory=ServletActionContext.getServletContext().getRealPath("/uploads");
-
- FileUtil.makeDir(directory);
-
-
for(inti=0,j=uploadFile.size();i<j;i++){
- fileName=uploadFileFileName.get(i);
- StringfilePath=directory+File.separator+fileName;
-
FileUtil.uploadFile(uploadFile.get(i),newFile(filePath));
- }
-
}catch(IOExceptione){
-
this.addActionMessage("");
- }
-
this.addActionMessage("文件上传成功!");
- }
-
return"fileUpload";
- }
//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代码如下:
- publicclassFileUtil{
-
privatestaticfinalintBUFFER_SIZE=16*1024;
-
publicstaticvoiduploadFile(Filesrc,Filedst)throwsIOException{
-
InputStreamin=null;
-
OutputStreamout=null;
-
try{
-
in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE);
-
out=newBufferedOutputStream(newFileOutputStream(dst),
- BUFFER_SIZE);
-
byte[]buffer=newbyte[BUFFER_SIZE];
-
while(in.read(buffer)>0){
- out.write(buffer);
- }
-
}finally{
-
if(null!=in){
- in.close();
- }
-
if(null!=out){
- out.close();
- }
- }
- }
-
publicstaticStringgetExtention(StringfileName){
-
intpos=fileName.lastIndexOf(".");
-
returnfileName.substring(pos);
- }
-
publicstaticvoidmakeDir(Stringdirectory){
-
Filedir=newFile(directory);
-
if(!dir.isDirectory()){
- dir.mkdirs();
- }
- }
-
publicstaticStringgenerateFileName(StringfileName)
-
throwsUnsupportedEncodingException{
-
DateFormatformat=newSimpleDateFormat("yyMMddHHmmss");
-
StringformatDate=format.format(newDate());
-
Stringextension=fileName.substring(fileName.lastIndexOf("."));
-
fileName=newString(fileName.getBytes("iso8859-1"),"gb2312");
-
returnfileName+"_"+formatDate+newRandom().nextInt(10000)
- +extension;
- }
- }
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文件;
分享到:
相关推荐
可以实现一次性上传多个文件,使用JavaScript动态添加一个上传文本框。是用struts2实现的
ajaxfileupload.js多文件上传和单文件,可传参,是做多图片上传时收集的,可进行多图片上传,进行后台传参,还有相关struts2相关示例.
java-springmvc-mybatis-easyuiSpringMvc3.2.x+mybatis3.1.x+EasyUI1.3.4+Maven架构的...编译环境:maven:3.x+当前示例演示了包括:权限控制、超大附件文件上传、EasyUI基本组件使用等等功能,具体请自行查看演示功能
实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...
实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...
实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...
实例34 随机读取文件程序示例 实例35 故事接龙 实例36 文件上传 实例37 在浏览器中打开文件 实例38 文件下载 实例39 用jspSmartUpload组件实现文件上传 实例40 应用jspSmartUpload组件进行...
2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...
2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
:springboot 原生上传文件操作(包含 springboot1.5 和 2.0 版本) :springboot整合lombok、elasticsearch、jersey、rabbitmq(整合之前需要安装相应的环境) :springboot 整合 websocket 可实现聊天室功能 :...
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...
Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...