smartupload -- 批量上传模块
2008-09-10 00:00
141 查看
啥也不说了,直接看代码
1. JSP --> 注意:注释标记的对应,例如三个<!-- *** -->下面的内容是对应的。
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>Upload Page</title>
<script type="text/javascript">
function addUpload(id_num) {
var file = document.getElementById(id_num+1);
file.style.display='block';
}
function displayTxt(id_num) {
var txt = document.getElementById(id_num);
txt.style.display='block';
}
</script>
</head>
<body>
<center>
<form action="user" method="post" enctype="multipart/form-data">
<table border="2" bgcolor="#ffff99">
<tr>
<td>Upload: </td>
<td> <!-- *** -->
<input type="file" name="upDoc" onChange="addUpload(0)">
<select value="重命名" onChange="displayTxt(10)">
<option>不命名</option>
<option>重命名</option>
</select>
</td>
<td><input id="10" name="0" style="display:none"></td>
</tr>
<%
for (int i = 1; i < 10; i++) {
%>
<!-- *** -->
<tr id="<%=i%>" style="display:none">
<td>Upload: </td>
<td> <!-- *** -->
<input type="file" name="upDoc" onChange="addUpload(<%=i%>)">
<!-- +++ -->
<select value="重命名" onChange="displayTxt(<%=10+i%>)">
<option>不命名</option>
<option>重命名</option>
</select>
</td> <!-- +++ --> <!-- 动态命名每个text -->
<td><input id="<%=10+i%>" name="<%=i*11%>" style="display:none"></td>
</tr>
<%
}
%>
<tr>
<td colspan="4" align="center">
<input type="hidden" name="status" value="upload">
<input type="submit" value="INSERT">
<input type="reset" value="RESET">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
2. Servlet
private ServletConfig config = null;
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// 1.准备工作
SmartUpload smart = null;
String status = null;
try {
smart = new SmartUpload();
smart.initialize(config, req, resp); //对smart进行初始化
smart.upload(); //smart准备工作, 我觉得也属于初始化工作,不知道为什么作者要把这个方法单独放出来
status = smart.getRequest().getParameter("status");
} catch (SmartUploadException sue) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
} catch (ServletException se) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
throw se;
} catch (IOException ioe) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
throw ioe;
}
// 2.批量上传 && 批量修改上传文件名
if("upload".equals(status)) {
for (int i = 0; i < smart.getFiles().getCount(); i++) {
if (!smart.getFiles().getFile(i).isMissing()) {
//int类型转String类型原来如此:Integer.toString(int i);
//i * 11是根据jsp中对"重命名text"的命名规律来定义的; 10个"重命名text"的name分别为"0"、"11"、"22"..."99"。
String upDocNameNum = Integer.toString(i * 11);
String upDocName = smart.getRequest().getParameter(upDocNameNum);
String ext = smart.getFiles().getFile(i).getFileExt();
try {
if ("".equals(upDocName)) {
//smart.save("/upload"); //将**全部**上传文件保存到指定目录下
/*
* 如果此处用smart.save(), 全部不修改文件名or全部修改文件名都没有问题;但部分修改/部分不修改的话会有bug, 修改了文件名的文件会按照"原文件名"和"修改后文件名"上传两份。
* 原因就是sava()是保存全部上传文件。实际上"全部不修改文件名"or"全部修改文件名"只是由于文件名冲突, 后面上传的覆盖了前面上传的而已。
* 如果部分修改, 每次循环都会复制全部的上传文件, 而且又没有文件名冲突。所以就会发生上面所诉的bug。
* 改正方法:使用我现在代码中的语句。
*/
smart.getFiles().getFile(i).saveAs("/upload/" + smart.getFiles().getFile(i).getFileName()); //不修改上传文件名时的保存方法
} else {
smart.getFiles().getFile(i).saveAs("/upload/" + upDocName + "." + ext); //修改上传文件名时的保存方法:这个方法不抛出ServletException异常
}
} catch (SmartUploadException sue) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
} catch (IOException ioe) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
throw ioe;
}
}
}
}
}
1. JSP --> 注意:注释标记的对应,例如三个<!-- *** -->下面的内容是对应的。
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>Upload Page</title>
<script type="text/javascript">
function addUpload(id_num) {
var file = document.getElementById(id_num+1);
file.style.display='block';
}
function displayTxt(id_num) {
var txt = document.getElementById(id_num);
txt.style.display='block';
}
</script>
</head>
<body>
<center>
<form action="user" method="post" enctype="multipart/form-data">
<table border="2" bgcolor="#ffff99">
<tr>
<td>Upload: </td>
<td> <!-- *** -->
<input type="file" name="upDoc" onChange="addUpload(0)">
<select value="重命名" onChange="displayTxt(10)">
<option>不命名</option>
<option>重命名</option>
</select>
</td>
<td><input id="10" name="0" style="display:none"></td>
</tr>
<%
for (int i = 1; i < 10; i++) {
%>
<!-- *** -->
<tr id="<%=i%>" style="display:none">
<td>Upload: </td>
<td> <!-- *** -->
<input type="file" name="upDoc" onChange="addUpload(<%=i%>)">
<!-- +++ -->
<select value="重命名" onChange="displayTxt(<%=10+i%>)">
<option>不命名</option>
<option>重命名</option>
</select>
</td> <!-- +++ --> <!-- 动态命名每个text -->
<td><input id="<%=10+i%>" name="<%=i*11%>" style="display:none"></td>
</tr>
<%
}
%>
<tr>
<td colspan="4" align="center">
<input type="hidden" name="status" value="upload">
<input type="submit" value="INSERT">
<input type="reset" value="RESET">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
2. Servlet
private ServletConfig config = null;
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// 1.准备工作
SmartUpload smart = null;
String status = null;
try {
smart = new SmartUpload();
smart.initialize(config, req, resp); //对smart进行初始化
smart.upload(); //smart准备工作, 我觉得也属于初始化工作,不知道为什么作者要把这个方法单独放出来
status = smart.getRequest().getParameter("status");
} catch (SmartUploadException sue) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
} catch (ServletException se) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
throw se;
} catch (IOException ioe) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
throw ioe;
}
// 2.批量上传 && 批量修改上传文件名
if("upload".equals(status)) {
for (int i = 0; i < smart.getFiles().getCount(); i++) {
if (!smart.getFiles().getFile(i).isMissing()) {
//int类型转String类型原来如此:Integer.toString(int i);
//i * 11是根据jsp中对"重命名text"的命名规律来定义的; 10个"重命名text"的name分别为"0"、"11"、"22"..."99"。
String upDocNameNum = Integer.toString(i * 11);
String upDocName = smart.getRequest().getParameter(upDocNameNum);
String ext = smart.getFiles().getFile(i).getFileExt();
try {
if ("".equals(upDocName)) {
//smart.save("/upload"); //将**全部**上传文件保存到指定目录下
/*
* 如果此处用smart.save(), 全部不修改文件名or全部修改文件名都没有问题;但部分修改/部分不修改的话会有bug, 修改了文件名的文件会按照"原文件名"和"修改后文件名"上传两份。
* 原因就是sava()是保存全部上传文件。实际上"全部不修改文件名"or"全部修改文件名"只是由于文件名冲突, 后面上传的覆盖了前面上传的而已。
* 如果部分修改, 每次循环都会复制全部的上传文件, 而且又没有文件名冲突。所以就会发生上面所诉的bug。
* 改正方法:使用我现在代码中的语句。
*/
smart.getFiles().getFile(i).saveAs("/upload/" + smart.getFiles().getFile(i).getFileName()); //不修改上传文件名时的保存方法
} else {
smart.getFiles().getFile(i).saveAs("/upload/" + upDocName + "." + ext); //修改上传文件名时的保存方法:这个方法不抛出ServletException异常
}
} catch (SmartUploadException sue) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
} catch (IOException ioe) {
System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
throw ioe;
}
}
}
}
}
相关文章推荐
- 使用jspSmartUpload组件进行文件上传、下载、批量下载
- jspSmartUpload实现图片的批量上传
- jspSmartUpload上传中文乱码问题
- jQuery.Uploadify插件实现带进度条的批量上传功能
- 基于servlet+smartUpload的文件上传
- JSP SmartUpload上传文件乱码解决纪实+UploadBean上传解决方案
- jspSmartUpload上传中文乱码解决
- SmartUpload上传su.initialize(this.getServletConfig(), req, response)语句异常
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- 关于jspsmartupload中文名文件上传后为乱码的问题
- jspSmartUpload上传下载全攻略
- 在jsp中使用smartupload组件上传文件
- JspSmartUpload上传中文时出现乱码的解决方法
- 上传下载全攻略jspSmartUpload
- jspsmartupload上传文件,中文乱码解决方法
- (分享)批量上传多图模块(非在线安装)
- 在jsp中使用jspSmartUpload实现上传下载全攻略(二)
- 基于jspSmartUpload的Jsp文件上传:防止文件重名被覆盖的解决方法
- jspSmartUpload上传下载全攻略(一)
- nginx上传模块—nginx upload module