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

📄 fileservice.java

📁 Java协同办公系统。实现公司内部的人事
💻 JAVA
字号:
package com.icss.oa.service;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import com.icss.oa.base.FileBase;
import com.icss.oa.utils.PageHelper;
import com.icss.oa.bean.FileVO;
import com.icss.oa.bean.UserSession;

public class FileService{
	public static List getFilesByUserId(int userId){
		String sqlStr="select * from t_file where personId="+userId+" order by path" ;
		List filesList=FileBase.fileQuery(sqlStr);
		return filesList;
	}
	
	public static boolean addFile(int userId,String disk,String filePath,String fileName){
		String path=filePath+fileName+"/";
		File file=new File(disk+path);
		System.out.println(filePath);
		boolean flag=false;
		if(!file.exists()){
			flag=file.mkdir();
		}
		if(flag){
			String sqlStr="insert into t_file values(fileid_seq.nextval,"+userId+",'"+fileName+"','"+path+"','"+0+"','"+0+"')";
			flag=FileBase.fileUpdate(sqlStr);
		}
		return flag;
	}
	public static void delFile(String disk,String path,int userId){
		try {
			delAllFile(new File(disk+path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sqlStr="delete from t_file where personid="+userId+" and path like '"+path+"%'";
		FileBase.fileUpdate(sqlStr);
		return;
		
	}
	public static void delAllFile(File f) throws IOException {
		if(!f.exists()){//文件夹不存在
			return;
		}
		boolean rslt=true;//保存中间结果
		if(!(rslt=f.delete())){//先尝试直接删除 若文件夹非空。枚举、递归删除里面内容
			File subs[] = f.listFiles();
			for (int i = 0; i <= subs.length - 1; i++) {
				if (subs[i].isDirectory()){
					delAllFile(subs[i]);//递归删除子文件夹内容
				}
				rslt = subs[i].delete();//删除子文件夹本身
			}
			rslt = f.delete();//删除此文件夹本身
			
		}
		return;
	}

	public static boolean upFile(HttpServletRequest request,String path) {
		String fileName=null;
		boolean flag=false;
		String upPath=request.getParameter("filePath");
		try {
			upPath=new String(upPath.getBytes("ISO-8859-1"),"GBK");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpSession session=request.getSession();
		UserSession user=(UserSession)session.getAttribute("usersession");
		int userId=user.getPersonId();
		try {
			DiskFileUpload fu = new DiskFileUpload(); 
//				 设置最大文件尺寸,这里是4MB
				//fu.setSizeMax(4194304);
//				 设置缓冲区大小,这里是4kb
				//fu.setSizeThreshold(4096); 
			List fileItems = fu.parseRequest(request); 
			Iterator i = fileItems.iterator(); 
			while(i.hasNext()) {
				FileItem fi = (FileItem)i.next(); 
				String fileName1 = fi.getName(); 
				if (fileName1 != null) {
					fileName = FilenameUtils.getName(fileName1);
					if (!FileBase.isFileExist(userId,upPath+fileName)){
						upPath=upPath+fileName;
						fi.write(new File(path+upPath));
						
						String sqlStr="insert into t_file values(fileid_seq.nextval,"+userId+",'"+fileName+"','"+upPath+"','"+1+"','"+0+"')";
						FileBase.fileUpdate(sqlStr);
						flag=true;
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			
		}		
		return flag;
	}
	public static PageHelper pageList(List filesList,int currPage,int pageSize){
		
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(currPage);
		pageHelper.setPageSize(pageSize);
		pageHelper.setRecordCount(filesList.size());
		int row=pageHelper.gotoPage(currPage);
		List list=new ArrayList();
		int j=0;
		for(int i=row;i<filesList.size()&&j<pageSize;i++,j++){
				list.add(filesList.get(i));
		}

		pageHelper.getPageCount(); 
		pageHelper.setPagebar("");
		pageHelper.setObjList(list);
		return pageHelper;
	}
	public static boolean renameFile(int fileId,String newFileName,String disk){
		boolean flag=false;
		String sqlStr="select * from t_file where fileId="+fileId;
		List filesList=FileBase.fileQuery(sqlStr);
		if(filesList.size()!=0){
			FileVO file=(FileVO)filesList.get(0);
			String oldFileName=file.getFileName();
			String oldFilePath=file.getPath();
			String newFilePath=oldFilePath.substring(0, oldFilePath.indexOf(oldFileName))+newFileName;
			if(file.getIsFile()==0){
				newFilePath=newFilePath+"/";
			}
			if(!FileBase.isFileExist(file.getUserId(), newFilePath)){
				File oldFile=new File(disk+oldFilePath);
				File newFile=new File(disk+newFilePath);
				if(oldFile.exists()&&(!newFile.exists())){
					flag=oldFile.renameTo(newFile);
				}
				//flag=new File(disk+oldFilePath).renameTo(new File(disk+newFilePath));
			}
			if(flag){
				String sqlUpdateStr1="update t_file set fileName='"+newFileName+"' , path='"+newFilePath+"' where fileid="+fileId+"";
				FileBase.fileUpdate(sqlUpdateStr1);
				String sqlUpdateStr2="update t_file set path=replace(path,'"+oldFileName+"','"+newFileName+"') where path like '"+oldFilePath+"%'";
				FileBase.fileUpdate(sqlUpdateStr2);
			}
			System.out.println(newFilePath);
		}
		return flag;
	}
	public static void shareFile(String filePath,int isShare){
		String sqlStr="update t_file set isShare="+isShare+" where path like '"+filePath+"%'";
		FileBase.fileUpdate(sqlStr);
		return;
	}
}

⌨️ 快捷键说明

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