復制代碼 代碼如下:
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body leftmargin="0" topmargin="0" bgcolor="#ffffff">
<form action="upfilea.jsp" method="post" name="form1"
enctype="multipart/form-data">
<input name="FileName" type="FILE" size="30">
<input type="submit" name="Submit" value="上傳" >
</form>
</body>
</html>
上傳頁面
<%@ 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 <mySmartUpload.getFiles().getCount();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(" <script language='javascript'><!--
alert('上傳失敗,圖片格式為
GIF或者JPG,視頻格式為WMV或者AVI,falsh為swf請重試!');
// --></script>");
out.print(" <script language='javascript'><!--
window.location.href =
'uploadb.jsp'
// --></script>");
}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(" <script
language='javascript'><!--
parent.heheForm.pic.value='"+pic+"';
// --></script>");
out.print(" <script language='javascript'><!--
alert('上傳成功');
// --></script>");
//下面的是將上載的文件保存到數據庫中
//將文件保存在服務器端
//將文件寫到數據庫中
//response.sendRedirect("uploada.jsp");
}}else{out.print(" <script language='javascript'><!--
alert('文件上傳失
敗!');location=''
// --></script>");}}
//與前面的if對應
%>
<html>
<head> </head>
<body leftmargin="0" topmargin="0" bgcolor="#ffffff">
<form>
<input type="text" name="pic" value=" <%=session.getAttribute("pic")%>"
size="30">
</form>
</body>
</html>