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

📄 fileaction.java

📁 网络硬盘
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//Created by MyEclipse Struts// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xslpackage com.zte.webfile.action;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Vector;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;import com.zte.webfile.dao.DAOFactory;import com.zte.webfile.dao.fileDAOInterface;import com.zte.webfile.dto.FileDTO;import com.zte.webfile.tool.UtilZip;import com.zte.webfile.dao.*;/** * MyEclipse Struts Creation date: 03-26-2007 *  * XDoclet definition: *  * @struts.action validate="true" */public class FileAction extends Action {	// --------------------------------------------------------- Instance	// Variables	// --------------------------------------------------------- Methods	/**	 * Method execute	 * 	 * @param mapping	 * @param form	 * @param request	 * @param response	 * @return ActionForward	 */	// 定义dao业务对象	private fileDAOInterface fileDAO;	// zip压缩对象	private UtilZip zip;	public fileDAOInterface getFileDAO() {		return fileDAO;	}	public void setFileDAO(fileDAOInterface fileDAO) {		this.fileDAO = fileDAO;	}	public UtilZip getZip() {		return zip;	}	public void setZip(UtilZip zip) {		this.zip = zip;	}	final Logger logger = Logger.getLogger(FileAction.class);	public ActionForward execute(ActionMapping mapping, ActionForm form,			HttpServletRequest request, HttpServletResponse response) {		// 定义页面跳转		String forword = new String();		// 得到用户信息		HttpSession session = request.getSession();		// 此处获取用户登陆后留在session中的用户名		String userName = (String) session.getAttribute("un");		logger.info("当用用户是:" + userName);		// 通过actionId判断不同的操作		String actionId = request.getParameter("actionId");		/*		 * 通过actionID信号判断不同的操作		 */		if (actionId.equals("upload")) { // 用于处理上传处理			forword = this.upload(request, response);		} else if (actionId.equals("rename")) { // 用于处理重命名操作		} else if (actionId.equals("newFolder")) { // 用于处理新建文件夹操作			forword = this.newFolder(request, response);		} else if (actionId.equals("download")) { // 用于单文件下载操作			forword = this.download(request, response);			return null;		} else if (actionId.equals("move")) { // 用于文件移动			forword = this.move(request, response);		} else if (actionId.equals("delete")) {			forword = this.delete(request, response);		} else if (actionId.equals("share")) {			forword = this.share(request, response);		} else if (actionId.equals("zipAndDown")) { // 打包下载			forword = this.zipAndDown(request, response);			return null;		}		return mapping.findForward(forword);	}	/**	 * 单文件或者多文件上传	 * 	 * @param request	 *            HttpServletRequest对象	 * @param response	 *            HttpServletResponse对象	 * @return 跳转信号	 */	public String upload(HttpServletRequest request,			HttpServletResponse response) {		// 定义页面跳转		String forword = new String();		// 得到用户信息		HttpSession session = request.getSession();		// 此处获取用户登陆后留在session中的用户名		String userName = (String) session.getAttribute("un");		// 获得要上传的父路径id		String parentPath1 = (String) request.getParameter("select2");		logger.info("要上传到文件夹的id为:" + parentPath1);		Integer parentPath = Integer.valueOf(parentPath1);		// 用smartUpload组件实现文件上传		SmartUpload su = new SmartUpload();		// 初始化变量		try {			su.initialize(this.getServlet().getServletConfig(), request,					response);		} catch (ServletException e1) {			// TODO 自动生成 catch 块			e1.printStackTrace();		}		// 文件上传		try {			try {				su.upload();			} catch (ServletException e) {				// TODO 自动生成 catch 块				e.printStackTrace();			}		} catch (IOException e) {			// TODO 自动生成 catch 块			e.printStackTrace();		} catch (SmartUploadException e) {			// TODO 自动生成 catch 块			e.printStackTrace();		}		// 用于存放每一个上传的文件,list中存放的FileDTO		List list = new ArrayList();		// 遍历没一个上传文件,获取其属性		for (int i = 0; i < su.getFiles().getCount(); i++) {			// 得到一个上传文件,并放入dto中			FileDTO dto = new FileDTO();			com.jspsmart.upload.File file = su.getFiles().getFile(i);			// 用户名			String fileName = file.getFileName();			// 文件扩展名			String fileExp = file.getFileExt();			// 文件路径			String filePath = file.getFilePathName();			// 文件大小			int size = file.getSize();			logger.info("上传文件信息:" + "文件名=》" + fileName + "文件路径=》" + filePath					+ "文件扩展名=》" + fileExp + "文件大小=》" + size + "上传人=》"					+ userName);			// 判断一下同名文件是否存在			boolean isExist = fileDAO.isExist(fileName, parentPath, userName);			// 如果存在给文件重命名			if (isExist) {				fileName = fileName + (1);			}			// 把信息放入dto			dto.setFileName(fileName);			dto.setFileOwner(userName);			dto.setFileParent(parentPath);// 设置父文件夹的id,注意这里是Integer			dto.setFileSize(Integer.valueOf(size));			dto.setFilePath("c:/upload/" + userName + "/" + fileName);			dto.setFileType(fileExp);			dto.setFshare("1"); // "0"表示文件夹,"1"表示文件			dto.setIsFolder("1");// "0"表示共享,"1"表示未共享			list.add(dto);			// 将文件写入磁盘			try {				file.saveAs("c:/upload/" + userName + "/" + file.getFileName(),						su.SAVE_PHYSICAL);			} catch (IOException e) {				// TODO 自动生成 catch 块				e.printStackTrace();			} catch (SmartUploadException e) {				// TODO 自动生成 catch 块				e.printStackTrace();			}		}		// 调用dao的方法把文件信息放入数据库		fileDAO.up(list);		// 查询文件已用容量		double content = DAOFactory.createSearchDAO().content(userName);// 查询文件容量		// 查询文件已用容量的比例		double present = DAOFactory.createSearchDAO().precent(content);		session.setAttribute("cont", content);		session.setAttribute("pre", present);		// 定义跳转指令		forword = "ok";		return forword;	}	/**	 * 单文件下载	 * 	 * @param request	 *            HttpServletRequest对象	 * @param response	 *            HttpServletResponse对象	 * @return 跳转信号	 */	public String download(HttpServletRequest request,			HttpServletResponse response) {		// 定义页面跳转		String forword = new String();		// 得到用户信息		HttpSession session = request.getSession();		// 此处获取用户登陆后留在session中的用户名		String userName = (String) session.getAttribute("un");		// 从页面中获取下载文件的id		String id = request.getParameter("downloadId");		logger.info("当前下载文件的用户==>>" + userName + "下载的文件id==>>" + id);		/*		 * 判断下载的是文件夹还是文件,如果是文件夹则调用打包下载方法,如果是文件则继续单文件下载		 */		int isFolder = fileDAO.isFileOrFolder(id);		logger.info("isFolder的值为==>>" + isFolder);		if (isFolder == 0) {			// 表明是下载的文件夹			logger.info("当前下载的是文件夹");			// 获得要下载的文件夹的信息			FileDTO fileDTO = fileDAO.getFileById(id);			// 打包后的文件名			String zipName = String.valueOf(fileDTO.getFileName()) + ".zip";			// 文件夹路径			String filePath = fileDTO.getFilePath();			String zipPath = "c:/upload/" + userName + "/" + zipName;			logger.info("压缩后的文件名" + zipName + "压缩后的压缩文件路径" + zipPath					+ "要压缩的文件路径" + filePath);			// // 从页面中获取要下载的文件			// String[] ids = new String[] { id };			// List fileList = new ArrayList();			// for (int i = 0; i < ids.length; i++) {			// FileDTO dto = fileDAO.getFileById(ids[i]);			// String filePath = dto.getFilePath();			// zipName = dto.getFileName() + ".zip";			// zip.zip(filePath, zipPath);			// }			// 对文件进行压缩处理			zip.zip(filePath, zipPath);			// 以下代码为下载此压缩包实现方法跟下载一个文件相似			// 定义输入			java.io.BufferedInputStream bis = null;			java.io.BufferedOutputStream bos = null;			try {				// 下载文件路径,全路径				String filepath = zipPath;				// 下载文件名				String filename = zipName;				logger.info("下载文件名=》" + filename + "\n下载文件路径" + filepath);				// 文件				File file = new File(filepath);				/**				 * 对文件进行编码,保证下载的文件格式的正确				 */				filename = new String(filename.getBytes("gb2312"), "ISO8859-1");				// 设置html中的ContentType,Header				response.setContentType("application/x-unknown;charset=gb2312");				response.setHeader("Content-disposition",						"attachment; filename=" + filename);				bis = new java.io.BufferedInputStream(						new java.io.FileInputStream(file));				bos = new java.io.BufferedOutputStream(response						.getOutputStream());				byte[] buff = new byte[2048];				int bytesRead;				// 进行文件下载,用I/O流				while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {					bos.write(buff, 0, bytesRead);					bos.flush();				}				// 清空response缓存区				response.flushBuffer();			} catch (Exception e) {				e.printStackTrace();			} finally {				if (bis != null)					try {						bis.close();					} catch (IOException e) {						// TODO 自动生成 catch 块						e.printStackTrace();					}				if (bos != null)					try {						bos.close();					} catch (IOException e) {						// TODO 自动生成 catch 块						e.printStackTrace();					}			}			// 下载完删除文件			File zipFile = new File(zipPath);			if (zipFile.exists()) {				zipFile.delete();			}		} else if (isFolder == 1) { // 当前下载的是文件			logger.info("当前下载的是文件");			// 实现单文件下载功能			java.io.BufferedInputStream bis = null;			java.io.BufferedOutputStream bos = null;			try {

⌨️ 快捷键说明

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