jsp SmartUpload 实现上传功能代码

复制代码 代码如下:

<%@ page contentType="text/html;charset=gb2312"%>





enctype="multipart/form-data">





上传页面
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new
com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i {
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
String myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
if(!(ext.equals("gif")||ext.equals("jpg")||ext.equals("wmv")
||ext.equals("avi")||ext.equals("swf"))){
out.print(" ");
out.print(" ");
}else
{
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath("/")+"upload/";
Random mynum=new Random(1);
int num=mynum.nextInt(10000);
num=(int)Math.random()*10000;
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat
("yyMMddhhmmss");
java.util.Date date=new java.util.Date();
String days=formatter.format(date);
days=days+num;
//System.out.println("days="+days);
//System.out.println("pre="+pre);
//取得别的参数
request.setCharacterEncoding("GB2312");
/*if(request.getSession(true).getAttribute("id")!="")
{
id = request.getSession(true).getAttribute("id").toString();
}
myFile.saveAs("upload/"+days+myFile.getFileName());
String pic="upload/"+days+myFile.getFileName();*/
myFile.saveAs("upload/"+days+"6."+ext);
String pic="upload/"+days+"6."+ext;
session.setAttribute("pic",pic);
out.print(" ");
out.print(" ");
//下面的是将上载的文件保存到数据库中
//将文件保存在服务器端
//将文件写到数据库中
//response.sendRedirect("uploada.jsp");
}}else{out.print(" ");}}
//与前面的if对应
%>




"
size="30">



你可能感兴趣的:(jsp SmartUpload 实现上传功能代码)