<html> <head><title>上传表单</title></head> <body> <form action="smartupload_demo01.jsp" method="post" enctype="multipart/form-data"> 请选择要上传的文件:<input type="file" name="pic"> <input type="submit" value="上传"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.bug.smart.*"%>
<html>
<head><title>接收图片,保存在根目录中的upload文件夹中</title></head>
<body>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload() ; // 上传准备
smart.save("upload") ; // 文件保存
%>
</body>
</html>
<html> <head><title>混合表单</title></head> <body> <form action="smartupload_demo02.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.bug.smart.*"%>
<%@ page import="cn.com.bug.util.*"%>
<html>
<head><title>接收封装表单的文本数据</title></head>
<body>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload() ; // 上传准备
String name = smart.getRequest().getParameter("uname") ;
smart.upload("upload");%>
<h2>姓名:<%=name%></h2>
<h2>request无法取得 : <%=request.getParameter("uname")%> </h2>
</body>
</html>
package cn.com.bug.util ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Random ;
public class IPTimeStamp {
private SimpleDateFormat sdf = null ; //定义SimpleDateFormat对象
private String ip = null ; //接收IP地址
public IPTimeStamp(){
}
public IPTimeStamp(String ip){ //得到IP地址+时间戳+三位随机数
this.ip = ip ;
}
public String getIPTimeRand(){
StringBuffer buf = new StringBuffer() ; //实例化StringBuffer对象
if(this.ip != null){
String s[] = this.ip.split("\.") ; //进行拆分操作
for(int i=0;i<s.length;i++){ //循环设置IP地址
buf.append(this.addZero(s[i],3)) ; //不够三位要补0
}
}
buf.append(this.getTimeStamp()) ; //取得时间戳
Random r = new Random() ; //定义Random对象,已产生随机数
for(int i=0;i<3;i++){ //循环三次
buf.append(r.nextInt(10)) ; //增加随机数
}
return buf.toString() //返回名称
}
public String getDate(){ //取得当前系统的时间
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
return this.sdf.format(new Date()) ;
}
public String getTimeStamp(){ //取得时间戳
this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
return this.sdf.format(new Date()) ;
}
private String addZero(String str,int len){ //补0操作
StringBuffer s = new StringBuffer() ;
s.append(str) ;
while(s.length() < len){
s.insert(0,"0") ;
}
return s.toString() ;
}
public static void main(String args[]){
System.out.println(new IPTimeStamp("192.168.1.1").getIPTimeRand()) ;
}
}
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.bug.smart.*"%>
<%@ page import="cn.com.bug.util.*"%>
<html>
<head><title>修改后的smartuoload.jsp</title></head>
<body>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload() ; // 上传准备
String name = smart.getRequest().getParameter("uname") ;
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ; // 取得客户端的IP地址
String ext = smart.getFiles().getFile(0).getFileExt() ; // 扩展名称
String fileName = its.getIPTimeRand() + "." + ext ;
smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;
%>
<%=smart.getFiles().getFile(0).getFileName().matches("^\w+.(jpg|gif)$")%>
<h2>姓名:<%=name%></h2>
<img src="../upload/<%=fileName%>">
</body>
</html>
if(smart.getFiles().getFile(0).getFileName().matches("^\w+\.(jsp|gif)$")){
//表示只允许后缀为jpg或gif的文件上传;
}
<html> <head><title>编写表单,上传3个文件</title></head> <body> <form action="smartupload_demo03.jsp" method="post" enctype="multipart/form-data"> 照片1:<input type="file" name="pic1"><br> 照片2:<input type="file" name="pic2"><br> 照片3:<input type="file" name="pic3"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.bug.smart.*"%>
<%@ page import="cn.com.bug.util.*"%>
<html>
<head><title>批量上传</title></head>
<body>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
SmartUpload smart = new SmartUpload() ;
smart.initialize(pageContext) ; // 初始化上传操作
smart.upload() ; // 上传准备
String name = smart.getRequest().getParameter("uname") ;
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ; // 取得客户端的IP地址
for(int x=0;x<smart.getFiles().getCount();x++){
String ext = smart.getFiles().getFile(x).getFileExt() ; // 扩展名称
String fileName = its.getIPTimeRand() + "." + ext ;
smart.getFiles().getFile(x).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;
}
%>
</body>
</html>
<html> <head><title>上传表单</title></head> <body> <form action="fileupload_demo01.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<html>
<head><title>接收上传文件</title></head>
<body>
<%
DiskFileItemFactory factory = new DiskFileItemFactory() ;
ServletFileUpload upload = new ServletFileUpload(factory) ;
upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M
List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
Iterator<FileItem> iter = items.iterator() ;//将全部的内容变为Iterator实例
while(iter.hasNext()){ //依次取出每一个内容
FileItem item = iter.next() ; //取出每一个上传的文件
String fieldName = item.getFieldName() ; // 取得表单控件的名称
%>
<ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4>
<%
if(!item.isFormField()){ // 不是普通文本
String fileName = item.getName() ; // 取得文件的名称
String contentType = item.getContentType() ; // 文件类型
long sizeInBytes = item.getSize() ;//文件大小
%>
<li>上传文件名称:<%=fileName%>
<li>上传文件类型:<%=contentType%>
<li>上传文件大小:<%=sizeInBytes%>
<%
} else {
String value = item.getString() ;
%>
<li>普通参数:<%=value%>
<%
}
%> </ul>
<%
}
%>
</body>
</html>
<html> <head><title>定义表单,可以同时上传多个文件</title></head> <body> <form action="fileupload_demo02.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic1"><br> 照片:<input type="file" name="pic2"><br> 照片:<input type="file" name="pic3"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="cn.com.bug.util.*"%>
<html>
<head><title>保存上传内容</title></head>
<body>
<%
DiskFileItemFactory factory = new DiskFileItemFactory() ;
factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ; // 设置一个临时文件
ServletFileUpload upload = new ServletFileUpload(factory) ;
upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M
List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
Iterator<FileItem> iter = items.iterator() ; //将全部的内容转换为Iterator实例
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;//实例化IP时间戳对象
while(iter.hasNext()){//依次取出每一个内容
FileItem item = iter.next() ; //取出每一个上传的文件
String fieldName = item.getFieldName() ; // 取得表单控件的名称
%>
<ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4>
<%
if(!item.isFormField()){ // 不是普通文本,是上传文件
File saveFile = null ; //定义保存的文件
InputStream input = null ;
OutputStream output = null ;
input = item.getInputStream() ;
output = new FileOutputStream(new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+its.getIPTimeRand()+
"."+item.getName().split("\.")[1])) ;//定义输入文件的路径
int temp = 0 ;
byte data[] = new byte[512] ;
while((temp=input.read(data,0,512))!=-1){ //依次读取内容
output.write(data) ; // 分块保存
}
input.close() ;
output.close() ;
} else {
String value = item.getString() ;//取出表单的内容
%>
<li>普通参数:<%=value%>
<%
}
%> </ul>
<%
}
%>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有