專案介紹
線上共享網盤採用jsp+servlet搭建專案結構實現共享網盤,專案分為管理員,普通使用者和付費使用者三種角色,根據不同角色控制不同許可權,實現不同使用者對個人檔案檔案,所有檔案,共享檔案的增刪改查操作。
專案適用人群正在做畢設的學生,或者需要專案實戰練習的Java學習者
開發環境:jdk 8intellij ideatomcat 8.5.40mysql 5.7所用技術:jsp+servletjs+ajaxlayUijdbc直連專案訪問地址http://localhost:8090
專案結構專案截圖註冊我的網盤我的共享回收站會員充值管理員-所有檔案管理員-共享申請關鍵程式碼:
1.初始化工作
//資料庫連線初始化public class DBInfo { String url = null; String username = null; String password = null; String driverClass = null; private static DBInfo db = new DBInfo(); public static DBInfo getInstance(){ return db; } private DBInfo() { InputStream in = this.getClass().getClassLoader().getResourceAsStream("db.properties"); Properties pp = new Properties(); try { pp.load(in); url = pp.getProperty("jdbc.url"); username = pp.getProperty("jdbc.username"); password = pp.getProperty("jdbc.password"); driverClass = pp.getProperty("jdbc.driver"); Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } public Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; }}//上傳資源初始化public void init() throws ServletException { super.init(); //servlet啟動時 ,讀取配置檔案中關於上傳的資訊 InputStream in = this.getClass().getClassLoader().getResourceAsStream("ini.properties"); Properties pp = new Properties(); try { pp.load(in); UPLOAD_ROOT_PATH = pp.getProperty("upload.path"); String tmpPath = pp.getProperty("tmp.path"); //配置上傳臨時目錄 factory = new DiskFileItemFactory(1024*1024*10,new File(tmpPath)); stu = new ServletFileUpload(factory); } catch (Exception e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } }}
2.資源上傳
//前端JSP程式碼<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadFile" aria-hidden="true"> <form action="upload" method="post" enctype="multipart/form-data"> <input type="hidden" name="from" value="user"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="uploadFile">上傳檔案</h4> </div> <div class="modal-body"> <input type="file" name="file" value="上傳檔案"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"> 關閉 </button> <input type="submit" class="btn btn-primary" value="確定上傳"/> </div> </div> </form> </div>//後端入庫處理protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { User user = (User) request.getSession().getAttribute(Const.SESSION_USER); String from=""; try { List<FileItem> fileItemLists = stu.parseRequest(request); for(FileItem fileItem : fileItemLists){ if(fileItem.isFormField()){ from = fileItem.getString(); }else{ //上傳檔名 String fileName = fileItem.getName(); String oldfilename = fileItem.getName(); int index = fileName.lastIndexOf("\\"); if(index != -1) { fileName = fileName.substring(index+1); } String root = UPLOAD_ROOT_PATH+user.getUsername(); //獲取檔案大小 long size = fileItem.getSize(); String sizeString = StringUtil.computeSize(size); Timestamp upTime = new Timestamp(new Date().getTime()); File file = new File(root,fileName); //解決檔案同名 int cnt = 1; while(file.exists()){ StringBuffer sb = new StringBuffer(fileName); sb.insert(sb.lastIndexOf("."), "("+cnt+")"); file = new File(root,sb.toString()); cnt++; } //檔案路徑是否存在 if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } try { fileItem.write(file); //上傳成功,資料庫儲存記錄 UserFile userFile = new UserFile(); userFile.setCreateTime(upTime); userFile.setFilename(file.getName()); userFile.setFilename(file.getName()); userFile.setFileSize(sizeString); userFile.setIsShared(0); userFile.setOwnerId(user.getId()); userFile.setPath(file.getAbsolutePath()); userFile.setOldfilename(oldfilename); userFileDao.save(userFile); response.sendRedirect(from+"?action=mydisk"); } catch (Exception e) { e.printStackTrace(); response.getWriter().print("上傳出錯"); } } } } catch (FileUploadException e) { e.printStackTrace(); response.setContentType("text/html; charset=utf8"); response.getWriter().print("上傳出錯!!"); }}
3.檢索重複上傳的資源
//這裡上傳在上面上傳資源時候,將儲存原始資源名字public List<UserFile> findRetrieveListByOwnerId(int ownerId,int isDelete){ List<UserFile> fileList = new ArrayList<UserFile>(); Connection conn = db.getConnection(); PreparedStatement ps = null; ResultSet rs = null; UserFile userFile = null; String sql="select * from file where oldfilename in ( " + " select a.oldfilename from (select oldfilename,count(id) counts from file GROUP BY oldfilename HAVING counts>1) a" + " ) and ownerid=? and isDelete=?"; ps = conn.prepareStatement(sql); ps.setInt(1, ownerId); ps.setInt(2, isDelete); rs = ps.executeQuery(); while(rs.next()){ userFile = new UserFile(); userFile.setId(rs.getInt(1)); userFile.setFilename(rs.getString(2)); userFile.setPath(rs.getString(3)); userFile.setCreateTime(rs.getTimestamp(4)); userFile.setIsShared(rs.getInt(5)); userFile.setOwnerId(rs.getInt(6)); userFile.setFileSize(rs.getString(7)); userFile.setCounts(rs.getInt(8)); userFile.setSharedReason(rs.getString("SharedReason")); userFile.setSharedTime(rs.getString("SharedTime")); fileList.add(userFile); } return fileList;}
4.平臺會員充值
最新評論