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

📄 uploadalbumservlet.java

📁 jsp+javabean写的论坛
💻 JAVA
字号:
package com.alumni.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.alumni.bean.*;
import com.jspsmart.upload.*;

public class UploadAlbumServlet extends HttpServlet
{
	ServletConfig config;
	//初始化
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.config=config;
	}
	//调用doPost方法
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		this.doPost(request,response);
	}
	//doPost核心方法
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		int userID=0;
		String note=null;
		String pubDate=null;
		String picName=null;
		
		DBConnection dbConn=null;
		Connection conn=null;
		Statement stmt=null;
		
		StringBuffer uploadSql=new StringBuffer();
		
		//session
		HttpSession session=request.getSession();
		//out
		PrintWriter out=response.getWriter();
		//application
		ServletContext application=this.getServletConfig().getServletContext();
		
		//使用SmartUpload组件
		SmartUpload su=null;
		Request req=null;
		
		//有没有上传文件
		boolean hasFile=false;
		
		//判断是否登陆过
		Boolean hasLogin=(Boolean)session.getAttribute("hasLogin");
		if(hasLogin!=null&&hasLogin.booleanValue()==true)
		{
			try
			{
				//打开数据库
				dbConn=new DBConnection();
				conn=dbConn.getConnectionToAccess(application.getRealPath("db/alumni.mdb"));
				stmt=conn.createStatement();
				
				//从SESSION中获取用户ID
				String uID=(String)session.getAttribute("userID");
				userID=Integer.parseInt(uID);
				//初始化SmartUpload对象
				su=new SmartUpload();
				su.initialize(config,request,response);
				//设定最大上传的文件大小
				su.setMaxFileSize(1000000);
				su.setTotalMaxFileSize(1000000);
				//设定允许上传的文件格式
				su.setAllowedFilesList("jpg,JPG,gif,GIF,png,PNG,jpeg,JPEG,bmp,BMP");
				su.upload();
				//获取表单数据对象
				req=su.getRequest();
				//利用SmartUpload的Request对象提取表单数据
				note=req.getParameter("picNote");
				pubDate=(new java.util.Date()).toLocaleString();
				//开始上传文件
				com.jspsmart.upload.Files files=su.getFiles();
				com.jspsmart.upload.File file=files.getFile(0);
				
				if(!file.isMissing())
				{
					//如果表单有值就另存为
					hasFile=true;
					String fileExt=file.getFileExt();
					//文件另存为
					java.util.Date time=new java.util.Date();
					Long preFile=new Long(time.getTime());
					String fileName=preFile.toString()+"."+fileExt;
					//获得上传之后的文件名
					picName=fileName;
					//StringBuffer uploadSql=new StringBuffer();
					uploadSql.append("INSERT INTO ALBUM(PIC_NAME,USER_ID,UPLOAD_DATE,PIC_NOTE) ");
					uploadSql.append("VALUES('");
					uploadSql.append(fileName);uploadSql.append("',");
					uploadSql.append(userID);uploadSql.append(",'");
					uploadSql.append(pubDate);uploadSql.append("','");
					uploadSql.append(note);uploadSql.append("')");
					
					//经验值+10
					stmt.executeUpdate(uploadSql.toString());
					String expSql="UPDATE USERINFO SET EXP=EXP+10 WHERE ID="+userID;
					stmt.executeUpdate(expSql);
					//保存文件
					file.saveAs("/upload/album/"+fileName,su.SAVE_VIRTUAL);
					
					//上传完毕后关闭数据库
					if(dbConn!=null)
					{
						dbConn.close();
					}
					response.sendRedirect("/album.jsp");
				}
				else
				{
					if(dbConn!=null)
					{
						dbConn.close();
					}
					response.sendRedirect("/error.jsp?code=NoFileException");
				}
			}
			catch(SmartUploadException s)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=SmartUploadException");
			}
			catch(SQLException se)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=SQLException");
			}
			catch(ClassNotFoundException cnte)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=ClassNotFoundException");
			}
			catch(Exception e)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=UnknownException");
			}
		}
		else
		{
			if(dbConn!=null)
			{
				dbConn.close();
			}
			//还未登陆
			response.sendRedirect("/login.jsp");
		}
	}
}

⌨️ 快捷键说明

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