jsp SmartUpload 實現上傳功能代碼_JSP編程

來源:互聯網
上載者:User
複製代碼 代碼如下:

<%@ 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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.