81,078
社区成员




<%@ page language="java"
import="java.util.*,java.io.*,org.apache.commons.fileupload.*"
pageEncoding="GBK"%>
<%
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>
<title>My JSP 'uploadJSPBack.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<%
response.setCharacterEncoding("GBK");
String uploadPath = "";
String tempPath = "d:\\";
try {
System.out.println("开始进行文件上传");
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath); // 设置临时目录
List fileItems = fu.parseRequest(request); // 得到所有的文件:
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
if (fileName != null) {
// 在这里可以记录用户和文件信息
// 此处可以定义一个接口(CallBack),用于处理后事。
// 写入文件a.txt,你也可以从fileName中提取文件名:
String name = fileName.substring(0, fileName
.indexOf("."));
String extfile = fileName.substring(fileName
.indexOf("."));
//上传时间作为文件名,用以防止重复上传
//Timestamp now = new Timestamp((new java.util.Date()).getTime());
//SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//String pfileName= fmt.format(now).toString().trim();
System.out.println(name + extfile);
fi.write(new File(name + extfile));
}
}
System.out.println("上传成功");
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("{success:true,message:'上传成功'}");
// 跳转到上传成功提示页面
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("{success:false,message:'上传失败'}");
// 可以跳转出错页面
}
%>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
uploadPic
</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
color: #0066FF;
}
-->
</style>
<script type="">
function check(){
var filePath1 = document.form1.file.value;
var filePath2 = document.form1.file2.value;
var filePath3 = document.form1.file3.value;
if((filePath1 == "") && (filePath2 == "") && (filePath3 == "")){
alert("您还没有选择上传的图片呢!");
return false;
}
else{
return true;
}
}
</script>
</head>
<body bgcolor="#ffffff">
<h1 align="center">广告信息的图片上传</h1>
<form action="Upload.jsp" method="post" enctype="multipart/form-data" name="form1" onsubmit="return check()">
<table width="420" height="169" align="center" cellpadding="0" border="1" cellspacing="0" style="border-bottom:1px solid">
<caption>选择多张图片后点击上传即可</caption>
<tr>
<td width="74"><div align="center"><span class="STYLE1">图片1</span>:</div></td>
<td width="340"><input name="file" type="file" size="30"></td>
</tr>
<tr>
<td height="31"><div align="center" class="STYLE1">图片2:</div></td>
<td><input name="file2" type="file" size="30"></td>
</tr>
<tr>
<td><div align="center" class="STYLE1">图片3:</div></td>
<td><input name="file3" type="file" size="30"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="上传">
<input type="reset" name="Submit2" value="取消"></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
<%@page contentType="text/html; charset=GBK"%>
<%@page import="com.jspsmart.upload.*"%>
<html>
<head>
<title>Upload</title>
</head>
<body bgcolor="#ffffff">
<%
/* 用此组件上传文件,若存在相同名称的文件,它会自动选择覆盖 */
try {
request.setCharacterEncoding("GBK");
SmartUpload su = new SmartUpload();
int countUpload = su.getFiles().getCount();
//初始化页面作用域
su.initialize(pageContext);
su.service(request, response);
//限制上传文件的类型,允许格式为 jpg,gif,bmp
su.setAllowedFilesList("jpg,gif,bmp");
//禁止文件上传类型
su.setDeniedFilesList("exe,bat,jsp,htm,html,");
su.upload();
//记录成功上传图片的数目
int count =0;
/*
* 如果想上传的图片以自己定义的名称保存的话,使用以下的方法
*
* 注意:使用这种方式保存的话,下面的 su.save("/image"); 此方法就不需要了,否则就重复保存了
int size = su.getFiles().getCount();
for(int i=0;i<size;i++){
File file = su.getFiles().getFile(i);
file.saveAs("/image/"+(i+1)+".jpg");
count++;
}*/
//将这些图片保存到站点下的 image 文件夹下,并得到成功上传文件数目
count = su.save("/image");
out.println("<script>");
out.println("alert('成功上传图片 "+count+" 张!');history.back(1);");
out.println("</script>");
}
catch (SmartUploadException ex) {
out.print(ex.toString());
}
catch(Exception e){
out.println("<script>");
out.println("alert('存在不允许上传的格式,只允许上传后缀为 jpg|gif|bmp 格式的图片,请重新选择文件!');history.back(1);");
out.println("</script>");
}
%>
</body>
</html>