国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

jsp SmartUpload 實(shí)現(xiàn)上傳功能代碼

2020-07-27 21:41:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(fù)制代碼 代碼如下:

<%@ 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>
上傳頁(yè)面
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//實(shí)例化上載bean
com.jspsmart.upload.SmartUpload mySmartUpload=new
com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//設(shè)置上載的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上載文件
mySmartUpload.upload();
//循環(huán)取得所有上載的文件
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請(qǐng)重試!');
// --></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);
//取得別的參數(shù)
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>");
//下面的是將上載的文件保存到數(shù)據(jù)庫(kù)中
//將文件保存在服務(wù)器端
//將文件寫(xiě)到數(shù)據(jù)庫(kù)中
//response.sendRedirect("uploada.jsp");
}}else{out.print(" <script language='javascript'><!--
alert('文件上傳失
敗!');location=''
// --></script>");}}
//與前面的if對(duì)應(yīng)
%>
<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>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 张家口市| 南昌县| 陵水| 宝鸡市| 康定县| 南木林县| 铜梁县| 柳江县| 黑水县| 汾西县| 韶山市| 嵩明县| 米泉市| 绥棱县| 高碑店市| 灵台县| 弥渡县| 洪江市| 阿城市| 皋兰县| 万盛区| 延长县| 海盐县| 措美县| 徐州市| 大理市| 普兰县| 越西县| 西安市| 竹山县| 莆田市| 建平县| 嫩江县| 英超| 墨竹工卡县| 辽源市| 丹凤县| 惠水县| 清原| 土默特右旗| 台北市|