fileaction.java
来自「网络硬盘」· Java 代码 · 共 664 行 · 第 1/2 页
JAVA
664 行
FileDTO dto = fileDAO.getFileById(id); String filepath = dto.getFilePath(); String filename = dto.getFileName(); logger.info("下载文件名=》" + filename + "\n下载文件路径" + filepath); // 文件 File file = new File(filepath); filename = new String(filename.getBytes("gb2312"), "ISO8859-1"); 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; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); bos.flush(); } 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(); } } } forword = "ok"; return forword; } /** * 文件打包下载 * * @param request * HttpServletRequest对象 * @param response * HttpServletResponse对象 * @return 跳转信号 */ public String zipAndDown(HttpServletRequest request, HttpServletResponse response) { // 定义页面跳转 String forword = new String(); // 得到用户信息 HttpSession session = request.getSession(); // 此处获取用户登陆后留在session中的用户名 String userName = (String) session.getAttribute("un"); // 打包后的文件名 String zipName = userName + ".zip"; String zipPath = "c:/upload/" + userName + "/" + zipName; // 从页面中获取要下载的文件 String[] ids = request.getParameterValues("myFile"); // 用于存放每个要压缩的文件. ArrayList fileList = new ArrayList(); for (int i = 0; i < ids.length; i++) { FileDTO dto = fileDAO.getFileById(ids[i]); fileList.add(dto); } // 进行压缩操作 zip.zip(fileList, 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(); } } // 下载完后删除此zip文件 File zipFile = new File(zipPath); if (zipFile.exists()) { zipFile.delete(); } forword = "ok"; return forword; } /** * 文件移动 * * @param request * HttpServletRequest对象 * @param response * HttpServletResponse对象 * @return 跳转信号 */ public String move(HttpServletRequest request, HttpServletResponse response) { // 定义页面跳转 String forword = new String(); // 得到用户信息 HttpSession session = request.getSession(); // 此处获取用户登陆后留在session中的用户名 String userName = (String) session.getAttribute("un"); // 获得要移动的文件夹id String parentPath1 = request.getParameter("check"); Integer parentPath = Integer.valueOf(parentPath1); // 获得要移动的文件id String[] id = request.getParameterValues("myFile"); Integer[] fileId = new Integer[id.length]; logger.info("要移动到的文件夹的id为:" + parentPath1); for (int i = 0; i < id.length; i++) { fileId[i] = Integer.valueOf(id[i]); logger.info("要移动的文件是:" + fileId[i]); } boolean bl = fileDAO.move(fileId, parentPath); if (bl) { System.out.println("移动成功!"); forword = "ok"; } else { System.out.println("移动失败!"); } // 重新更新session中的文件夹信息 Vector vt = DAOFactory.createSearchDAO().searchID(userName); if (vt.size() != 0) { Integer id1 = null; Integer number = null; for (int i = 0; i < vt.size(); i++) { Vector res = (Vector) vt.get(i); number = Integer.valueOf(res.get(0).toString()); id1 = number.intValue(); } // 定义hashmap接受获取的 HashMap map = DAOFactory.createSearchDAO().searchFilePath(userName, id1); // 放入session中关于文件夹的信息 session.setAttribute("map", map); } return forword; } /** * 文件删除操作 * * @param request * HttpServletRequest对象 * @param response * HttpServletResponse对象 * @return 跳转信号 */ public String delete(HttpServletRequest request, HttpServletResponse response) { // 定义页面跳转 String forword = new String(); // 得到用户信息 HttpSession session = request.getSession(); // 此处获取用户登陆后留在session中的用户名 String userName = (String) session.getAttribute("un"); // 获得要删除的文件id String[] fileId = request.getParameterValues("myFile"); Integer[] id = new Integer[fileId.length]; for (int i = 0; i < fileId.length; i++) { id[i] = Integer.valueOf(fileId[i]); } boolean bl = fileDAO.delete(id); if (bl) { // 删除成功 System.out.println("删除成功!"); } else { // 删除失败 System.out.println("删除失败!"); } forword = "ok"; // 查询文件已用容量 double content = DAOFactory.createSearchDAO().content(userName);// 查询文件容量 // 查询文件已用容量的比例 double present = DAOFactory.createSearchDAO().precent(content); session.setAttribute("cont", content); session.setAttribute("pre", present); // 重新更新session中的文件夹信息 Vector vt = DAOFactory.createSearchDAO().searchID(userName); if (vt.size() != 0) { Integer id1 = null; Integer number = null; for (int i = 0; i < vt.size(); i++) { Vector res = (Vector) vt.get(i); number = Integer.valueOf(res.get(0).toString()); id1 = number.intValue(); } // 定义hashmap接受获取的 HashMap map = DAOFactory.createSearchDAO().searchFilePath(userName, id1); // 放入session中关于文件夹的信息 session.setAttribute("map", map); } return forword; } /** * 文件共享 * * @param request * HttpServletRequest对象 * @param response * HttpServletResponse对象 * @return 跳转信号 */ public String share(HttpServletRequest request, HttpServletResponse response) { // 定义页面跳转 String forword = new String(); // 得到用户信息 HttpSession session = request.getSession(); // 此处获取用户登陆后留在session中的用户名 String userName = (String) session.getAttribute("un"); // 从页面上获取信息 String[] ids = request.getParameterValues("myFile"); Integer[] fileId = new Integer[ids.length]; for (int i = 0; i < fileId.length; i++) { fileId[i] = Integer.valueOf(ids[i]); logger.info("当前要共享的文件id==>" + fileId[i]); } // 调用业务方法 boolean bl = fileDAO.share(fileId); if (bl) { // 共享成功 System.out.println("共享成功!"); } else { System.out.println("共享失败!"); } forword = "ok"; return forword; } /** * 新建文件夹 * * @param request * HttpServletRequest对象 * @param response * HttpServletResponse对象 * @return 跳转信号 */ public String newFolder(HttpServletRequest request, HttpServletResponse response) { // 定义页面跳转 String forword = new String(); // 得到用户信息 HttpSession session = request.getSession(); // 此处获取用户登陆后留在session中的用户名 String userName = (String) session.getAttribute("un"); // 从表单获取数据 String folderName = request.getParameter("newFolderName"); String parentPath1 = request.getParameter("select2"); Integer parentPath = Integer.valueOf(parentPath1); // 调用dao中创建文件夹的方法 logger.info("文件夹名称:" + folderName + "parentPath:" + parentPath + "userName:" + userName); boolean bl = fileDAO.createFolder(folderName, parentPath, userName); if (bl) { System.out.println("创建成功!"); forword = "ok"; } else { System.out.println("创建失败!"); } // 在实际物理文件上建立文件夹 new File("c:/upload/" + userName + "/" + folderName).mkdir(); // 重新更新session中的文件夹信息 Vector vt = DAOFactory.createSearchDAO().searchID(userName); if (vt.size() != 0) { Integer id = null; Integer number = null; for (int i = 0; i < vt.size(); i++) { Vector res = (Vector) vt.get(i); number = Integer.valueOf(res.get(0).toString()); id = number.intValue(); } // 定义hashmap接受获取的 HashMap map = DAOFactory.createSearchDAO().searchFilePath(userName, id); // 放入session中关于文件夹的信息 session.setAttribute("map", map); } return forword; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?