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

📄 upfiledaoim.java

📁 文章管理系统 用Java开发,以Struts为框架,以Jdbc链接Mysql数据库。系统属性:系统属性设置、留言管理、友情链接管理、网站调查、公告管理 关于我们、版权声明、联系我们
💻 JAVA
字号:
package com.yhcms.manage.upload.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.yhcms.db.DBConnException;
import com.yhcms.db.DBConnect;
import com.yhcms.manage.upload.bean.UpFile;
import com.yhcms.manage.upload.itface.UpFileDao;
/**
 * <p>Title:系统上传文件的相关操作实现</p>
 * <li>上传文件与数据库相关的各项操作</li>
 * <b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YH-2.0
 */
public class UpFileDaoIm implements UpFileDao {

	private static Logger yhlog = Logger.getLogger(UpFileDaoIm.class.getName());
	
	private DBConnect dbconn = null;
	
	private static UpFileDaoIm filedao = new UpFileDaoIm();
	
	/**
	 * @return 取得一个系统文件操作对象
	 */
	public static UpFileDaoIm getInstance(){
		return filedao;
	}
	
	public boolean uploadFile(UpFile file) throws DBConnException {
		int i = 0;
		// 取得系统文章最大Id+1 作为新录入文章的Id
		int maxid = getFileMaxId()+1;
		UpFile curFile = file;
		int artid = 0;
		int author = 0;
		String filetype = "";
		String filename = "";
		int filesize = 0;
		String realname = "";
		String ptime = "";
		String path = "";
		int width = 0 ;
		int height = 0;
		int downtimes = 0;
		artid = curFile.getArtid();
		author = curFile.getAuthor();
		filetype = curFile.getFiletype();
		filename = curFile.getFilename();
		filesize = curFile.getFilesize();
		realname = curFile.getRealname();
		ptime = curFile.getPtime();
		path = curFile.getPath();
		width = curFile.getWidth();
		height = curFile.getHeight();
		downtimes = curFile.getDowntimes();
		
		
		String sql = "insert into upload(id,artid,author,filename,filetype,filesize,realname,ptime,path," +
					 "width,height,downtimes) values ("+maxid+","+artid+",'"+author+"','"+filename+"','"+filetype+"','"+
					 filesize+"','"+realname+"','"+ptime+"','"+path+"','"+width+"','"+ height+"','"+downtimes+"')";
		try{
			dbconn = new DBConnect(sql);
			i = dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When upload a file,throw an Exception!");
		}finally{
			dbconn.close();
		}
		if(i>0){
			yhlog.info("Upload a file successfully.the file id is:"+maxid+".");
			return true;
		}else{
			yhlog.info("Upload a file unsuccessfully.");
			return false;
		}
	}

	public boolean delFile(int id) throws DBConnException {
		int i = 0;
		String sql = "delete from upload where id="+id;
		try{
			dbconn = new DBConnect(sql);
			i = dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When delete a file,throw an Exception!The file id is:"+id+".");
		}finally{
			dbconn.close();
		}
		if(i>0){
			yhlog.info("Delete a file successfully,the file id is:"+id+".");
			return true;
		}else{
			yhlog.info("Delete a file unsuccessfully.the file id is:"+id+".");
			return false;
		}
	}

	public int getFileMaxId() throws DBConnException {
		int maxId = 0;
		String sql = "select max(id) from upload";
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			if(dbconn.next()){
				maxId = dbconn.getInt(1);
			}
		}catch(Exception e){
			yhlog.warn("When get the max id of system upload file,throw an Exception!");
		}finally{
			dbconn.close();
		}
		return maxId;
	}

	public void downAddTime(int id) throws DBConnException {
		String sql = "update upload set downtimes=downtimes+1 where id="+id;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When update a file downtimes,throw an Exception!");
		}finally{
			dbconn.close();
		}
		
	}

	public List getAllFiles(int begin, int size) throws DBConnException {
		ArrayList filelist = new ArrayList();
		UpFile curFile = null;
		String sql = "select f.id,f.artid,f.author,f.filename,f.filetype,f.filesize,f.realname,f.ptime," +
				" f.path,f.width,f.height,f.downtimes from upload as f order by f.id desc limit "+begin+","+size;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			while(dbconn.next()){
				curFile = new UpFile();
				curFile.setId(dbconn.getInt(1));
				curFile.setArtid(dbconn.getInt(2));
				curFile.setAuthor(dbconn.getInt(3));
				curFile.setFilename(dbconn.getString(4));
				curFile.setFiletype(dbconn.getString(5));
				curFile.setFilesize(dbconn.getInt(6));
				curFile.setRealname(dbconn.getString(7));
				curFile.setPath(dbconn.getString(8));
				curFile.setWidth(dbconn.getInt(9));
				curFile.setHeight(dbconn.getInt(10));
				curFile.setDowntimes(dbconn.getInt(11));
				
				filelist.add(curFile);
			}
		}catch(Exception e){
			yhlog.warn("when get all upload files,throw an Exception!");
		}finally{
			dbconn.close();
		}
		return filelist;
	}

	public List getArtFiles(int artid) throws DBConnException {
		ArrayList filelist = new ArrayList();
		UpFile curFile = null;
		String sql = "select f.id,f.author,f.filename,f.filetype,f.filesize,f.realname,f.ptime," +
					 " f.path,f.width,f.height,f.downtimes from upload as f where f.artid="+artid+" order by f.id desc";
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			while(dbconn.next()){
				curFile = new UpFile();
				curFile.setId(dbconn.getInt(1));
				curFile.setAuthor(dbconn.getInt(2));
				curFile.setFilename(dbconn.getString(3));
				curFile.setFiletype(dbconn.getString(4));
				curFile.setFilesize(dbconn.getInt(5));
				curFile.setRealname(dbconn.getString(6));
				curFile.setPtime(dbconn.getString(7));
				curFile.setPath(dbconn.getString(8));
				curFile.setWidth(dbconn.getInt(9));
				curFile.setHeight(dbconn.getInt(10));
				curFile.setDowntimes(dbconn.getInt(11));
				
				filelist.add(curFile);
			}
		}catch(Exception e){
			yhlog.warn("When get one article's all upload files,throw an Exception!The article id is:"+artid+".");
		}finally{
			dbconn.close();
		}
		return filelist;
	}

	public void updateArtFiles(int artid,int userid) throws DBConnException {
		
		String sql = "update upload set artid="+artid+" where artid=0 and author="+userid;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When update a article's files,throw an Exception!");
		}finally{
			dbconn.close();
		}
		
	}
	
}

⌨️ 快捷键说明

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