jsp SmartUpload 實(shí)現(xiàn)上傳功能代碼
更新時(shí)間:2009年06月24日 11:30:31 作者:
jsp SmartUpload 實(shí)現(xiàn)上傳的代碼,需要的朋友可以參考下。
復(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ù)器端
//將文件寫到數(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>
您可能感興趣的文章:
相關(guān)文章
jsp獲取action傳來(lái)的session和session清空以及判斷
這篇文章主要介紹了jsp獲取action傳來(lái)的session和session清空以及判斷,需要的朋友可以參考下2014-03-03JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法
這篇文章主要介紹了JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法,涉及JSP文件傳輸及目錄操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交
這篇文章主要介紹了詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交的相關(guān)資料,需要的朋友可以參考下2017-06-06用Java實(shí)現(xiàn)FTP服務(wù)器解決方案
用Java實(shí)現(xiàn)FTP服務(wù)器解決方案...2006-10-10詳解JSP中的語(yǔ)句對(duì)象Statement操作MySQL的使用實(shí)例
這篇文章主要介紹了JSP中的語(yǔ)句對(duì)象Statement操作MySQL的使用實(shí)例,講解了添加記錄、更新記錄、刪除記錄三種操作,說(shuō)明都在代碼注釋中簡(jiǎn)單明了,需要的朋友可以參考下2016-04-04使用MongoDB和JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的購(gòu)物車系統(tǒng)實(shí)例
本篇文章主要介紹了使用MongoDB和JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的購(gòu)物車系統(tǒng)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11jsp中獲取狀態(tài)怎么寫(兩種實(shí)現(xiàn)方式)
由于框架是2005年的,jsp中不能存放標(biāo)簽,只能有java代碼來(lái)寫了,接下來(lái)為大家介紹下兩種實(shí)現(xiàn)獲取狀態(tài)寫法,感興趣的各位可以參考下哈,希望可以幫助到你2013-04-04