jsp SmartUpload upload function code

  • 2020-05-09 19:03:40
  • OfStack


<%@ 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=" upload " > 
</form> 
</body> 
</html> 
 The upload page  
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*"%> 
<%@ page import="java.util.*"%> 
<%@ page import="java.io.*"%> 
<%@ page import="com.jspsmart.upload.*" %> 
<% 
// Instantiated upload bean 
com.jspsmart.upload.SmartUpload mySmartUpload=new 
com.jspsmart.upload.SmartUpload(); 
// Initialize the  
mySmartUpload.initialize(pageContext); 
// Set the maximum upload value  
mySmartUpload.setMaxFileSize(500 * 1024*1024); 
// Upload file  
mySmartUpload.upload(); 
// Loop to get all uploaded files  
for (int i=0;i <mySmartUpload.getFiles().getCount();i++) 
{ 
// Gets the uploaded file  
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); 
if (!myFile.isMissing()) 
{ 
// Gets the file name of the uploaded file  
String myFileName=myFile.getFileName(); 
// Gets a file name without a suffix  
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.')); 
// Get a suffix name  
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(' Upload failed, the image format is  
GIF or JPG , the video format is WMV or AVI . falsh for swf Please try again! '); 
// --></script>"); 
out.print(" <script language='javascript'><!-- 
window.location.href = 
'uploadb.jsp' 
// --></script>"); 
}else 
{ 
// Gets the size of the file  
int fileSize=myFile.getSize(); 
// Save the path  
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); 
// Get other parameters  
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(' Uploaded successfully '); 
// --></script>"); 
// The following is to save the uploaded file to the database  
// Save the file on the server side  
// Write the file to the database  
//response.sendRedirect("uploada.jsp"); 
}}else{out.print(" <script language='javascript'><!-- 
alert(' File upload lost  
 Defeat! ');location='' 
// --></script>");}} 
// As in the previous if The corresponding  
%> 
<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> 

Related articles: