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

📄 uploadbiz.java

📁 一个不错的bbs论坛系统.对初学者很有帮助
💻 JAVA
字号:
package com.yhbbs.upload.biz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;
import com.yhbbs.upload.dao.UpFileDaoIm;
import com.yhbbs.upload.itface.UpFile;
import com.yhbbs.upload.itface.UpFileDao;
/**
* <p>Title:上传文件Action</p>
* <li>上传文件模块</li>
* <br><b>WebSite: www.yyhweb.com</b>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YHBBS-2.0
*/
public class UpLoadBiz{
	
	private static Logger bbslog = Logger.getLogger(UpLoadBiz.class);
	
	private static UpFileDao filedao = UpFileDaoIm.getInstance();
	
	/** 取得上传附件最大Id
	 * @return int 最大Id 
	 */
	public static int getMaxId(){
		 
		try {
			 return filedao.getFileMaxId();
	        }catch(SQLException e) {
	            bbslog.error("Throws a SqlException when invoke getMaxId():\n" + e.toString());
	        }
	        return 0;
	}
	/** 根据帖子Id和用户Id,取得该用户发表某帖子时所有上传附件(包括以上传但未发表的)
	 * @param artId 帖子Id
	 * @param userId 用户Id
	 * @return List 附件列表 
	 */
	public static List getArtFile(int artId,int userId){
		
		try {
			return filedao.getArtFiles(artId,userId);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArtFile(int artId,int userId):\n" + e.toString());
		}
		return null;
	}
	
	/** 取得用户上传附件总数
	 * @param userId 用户Id
	 * @return int 用户上传附件总数
	 */
	public static int getUserFileCount(int userId){
		 
		try {
			 return filedao.getUserFileCount(userId);
	        }catch(SQLException e) {
	            bbslog.error("Throws a SqlException when invoke getUserFileCount(int userId):\n" + e.toString());
	        }
	        return 0;
	}
	
	/** 取得用户上传附件总大小
	 * @param userId 用户Id
	 * @return int 用户上传附件总大小
	 */
	public static int getUserFileSize(int userId){
		
		try {
			return filedao.getUserFileSize(userId);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserFileSize(int userId):\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得用户某段上传附件
	 * @param userid 用户Id
	 * @param start 开始位置
	 * @param end 查询长度
	 * @return List 附件列表 
	 */
	public static List getUserFile(int userid,int start,int end){
		HashMap<String,Integer> sizeMap = new HashMap<String,Integer>();
		sizeMap.put("userid", userid);
		sizeMap.put("start", start);
		sizeMap.put("end", end);
		try {
			return filedao.getUserFiles(sizeMap);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserFile(int userid,int start,int end):\n" + e.toString());
		}
		return null;
	}
	
	/** 将上传文件信息写入数据库
	 * @param file 当前上传文件
	 */
	public static void upLoadFile(UpFile file){
		
		try {
			file.setId((getMaxId()+1));
			filedao.upLoadFile(file);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke upLoadFile(UpFile file):\n" + e.toString());
		}
		return ;
	}
	
	/** 发表文章时,更新文章附件信息
	 * @param artId 帖子Id
	 * @param userId 用户Id
	 */
	public static void postArtFile(int artId,int userId){
		
		try {
			filedao.postArtFiles(artId, userId);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke postArtFile(int artId,int userId):\n" + e.toString());
		}
		return ;
	}
	
	/** 删除数据库中相关附件
	 * @param Id 文件Id
	 * @return 成功:true 失败:false
	 */
	public static boolean delOneFile(int Id){
		
		try {
			return filedao.delOneFile(Id);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke delOneFile(int Id):\n" + e.toString());
		}
		return false;
	}
	
	/** 将上传文件信息写入数据库
	 * @param artId 帖子Id
	 * @return 成功:true 失败:false
	 */
	public static boolean delArtFile(int artId){
		
		try {
			return filedao.delArtFile(artId);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke delArtFile(int Id):\n" + e.toString());
		}
		return false;
	}
	/**
	 * @param curFile 当前上传文件
	 * @param filePath 写入系统的路径
	 * @param file 系统上传文件
	 * @param db 是否写入数据库 1:写入 0:不写
	 * @return 成功:true 失败:false
	 */
	public static boolean writeFile(FormFile curFile, String filePath,UpFile file,int db) {
		boolean noExcp = true;
		InputStream input = null;
		OutputStream output = null;
		try {
			input = curFile.getInputStream();
			output = new FileOutputStream(filePath);
			int size = 0;
			byte temp[] = new byte[8192];
			while ((size = input.read(temp, 0, 8192)) != -1)
				output.write(temp, 0, size);
			temp = (byte[]) null;
			
		}catch (Exception e) {
			noExcp = false;
			e.printStackTrace();
		}
		finally {
			try {
				if (input != null) {
					input.close();
					input = null;
				}
				if (output != null) {
					output.close();
					output = null;
				}
				if (curFile != null) {
					curFile.destroy();
					curFile = null;
				}
			}catch (Exception e) {
				noExcp = false;
				e.printStackTrace();
			}

		}
		if(noExcp && db==1){
			upLoadFile(file);
		}
		return noExcp;
	}

	/** 删除系统文件
	 * @param filePath 文件路径
	 * @param fileId 文件Id
	 * @return 成功:true 失败:false
	 */
	public static boolean delFile(String filePath,int fileId) {
		try {
			File delfile = new File(filePath);
			boolean temp = false;
			if (delfile.exists())
				temp = delfile.delete();
			delfile = null;
			delOneFile(fileId);
			return temp;
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
//	public static void uploadDao(UpFile curFile){
//		boolean flag = false;
//		UpFileDao fileDao = UpFileDaoIm.getInstance();
//			try {
//				flag = fileDao.uploadFile(curFile);
//			}
//			catch (DBConnException e) {
//				yhlog.warn("Add a upload file information,throw an Exception!File name is:"+curFile.getFilename());
//			}
//			if(!flag){
//				yhlog.warn("Add a upload file information unsuccessfully.File name is:"+curFile.getFilename());
//			}
//		return ;
//	}
//	
//	public static void deleteDao(int fileId){
//		boolean flag = false;
//		UpFileDao fileDao = UpFileDaoIm.getInstance();
//		try {
//			flag = fileDao.delFile(fileId);
//		}
//		catch (DBConnException e) {
//			yhlog.warn("Delete a upload file information,throw an Exception!File id is:"+fileId);
//		}
//		if(!flag){
//			yhlog.warn("Delete a upload file information unsuccessfully.File id is:"+fileId);
//		}
//		return ;
//	}
}

⌨️ 快捷键说明

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