⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadimage.jsp

📁 在线教育平台: 从小学
💻 JSP
字号:
<%@ page contentType="text/html;charset=gb2312" language="java"
	import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,com.jspsmart.upload.*,java.util.*"%>
<jsp:useBean id="user" scope="page" class="edu.UserBean" />
<jsp:useBean id="teacher" scope="page" class="edu.TeacherBean" />
<jsp:useBean id="admin" scope="page" class="edu.AdminBean" />
<% 
int userid=0;
String sort=null;
try{
	sort=session.getAttribute("sort").toString();
	  userid=Integer.valueOf(session.getAttribute("userid").toString()).intValue();
}catch(Exception e){}


SmartUpload mySmartUpload =new SmartUpload(); 
long file_size_max=100000; 
String fileName2="",ext="",testvar=""; 
String url="uploadfile/images/"; //应保证在根目录中有此目录的存在 
//初始化 
mySmartUpload.initialize(pageContext); 
//只允许上载此类文件 
try { 
mySmartUpload.setAllowedFilesList("jpg,gif"); 
//上载文件 
mySmartUpload.upload(); 
} catch (Exception e){ 
%>
<SCRIPT language=javascript> 
alert("只允许上传.jpg和.gif类型图片文件"); 
window.location='javascript:history.back();'; 
</script>
<% 
} 
try{ 

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); 
if (myFile.isMissing()){%>
<SCRIPT language=javascript> 
alert("请先选择要上传的文件"); 
window.location='javascript:history.back();'; 
</script>
<%} 
else{ 
//String myFileName=myFile.getFileName(); //取得上载的文件的文件名 
ext= myFile.getFileExt(); //取得后缀名 
int file_size=myFile.getSize(); //取得文件的大小 
String saveurl=""; 
if(file_size<file_size_max){ 
//更改文件名,取得当前上传时间的毫秒数值 
Calendar calendar = Calendar.getInstance(); 
String filename = String.valueOf(calendar.getTimeInMillis()); 
saveurl=request.getRealPath("/")+url; 
saveurl+=filename+"."+ext; //保存路径 
myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL); 
//out.print(filename); 
//-----------------------上传完成,开始生成缩略图------------------------- 
java.io.File file = new java.io.File(saveurl); //读入刚才上传的文件 
String newurl=request.getRealPath("/")+url+filename+"_min."+ext; //新的缩略图保存地址 
String photo=filename+"_min."+ext;
if(sort.equalsIgnoreCase("user")){
user.setPhoto(photo);
user.setId(userid);
user.updatePhoto();}
if(sort.equalsIgnoreCase("teacher")){
	teacher.setPhoto(photo);
	teacher.setId(userid);
	teacher.updatePhoto();	
}
if(sort.equalsIgnoreCase("admin")){
	admin.setPhoto(photo);
	admin.setId(userid);
	admin.updatePhoto();	
}
Image src = javax.imageio.ImageIO.read(file); //构造Image对象 
float tagsize=120; 
int old_w=src.getWidth(null); //得到源图宽 
int old_h=src.getHeight(null); 
int new_w=0; 
int new_h=0; //得到源图长 
int tempsize; 
float tempdouble; 
if(old_w>old_h){ 
tempdouble=old_w/tagsize; 
}else{ 
tempdouble=old_h/tagsize; 
} 
new_w=Math.round(old_w/tempdouble); 
new_h=Math.round(old_h/tempdouble);//计算新图长宽 
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB); 
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图 
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流 
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage); 
encoder.encode(tag); //近JPEG编码 
newimage.close(); 
if(sort.equalsIgnoreCase("user")){
	response.sendRedirect("modifyuser.jsp");
}
	if(sort.equalsIgnoreCase("teacher")){
		response.sendRedirect("modifyteacher.jsp");
	}
	if(sort.equalsIgnoreCase("admin")){
		response.sendRedirect("modifyadmin.jsp");
	}
} 
else{ 
out.print("<SCRIPT language='javascript'>"); 
out.print("alert('上传文件大小不能超过"+(file_size_max/1000)+"K');"); 
out.print("window.location='javascript:history.back();'"); 
out.print("</SCRIPT>"); 
} 
} 
}catch (Exception e){ 

e.toString(); 

} 
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -