欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手把手搭建Java共享網(wǎng)盤的方法步驟

 更新時(shí)間:2020年12月29日 09:23:56   作者:程序幫  
這篇文章主要介紹了手把手搭建Java共享網(wǎng)盤,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

項(xiàng)目介紹

在線共享網(wǎng)盤采用jsp+servlet搭建項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)共享網(wǎng)盤,項(xiàng)目分為管理員,普通用戶和付費(fèi)用戶三種角色,根據(jù)不同角色控制不同權(quán)限,實(shí)現(xiàn)不同用戶對(duì)個(gè)人文件文件,所有文件,共享文件的增刪改查操作。

項(xiàng)目適用人群

正在做畢設(shè)的學(xué)生,或者需要項(xiàng)目實(shí)戰(zhàn)練習(xí)的Java學(xué)習(xí)者

開發(fā)環(huán)境:

  • jdk 8
  • intellij idea
  • tomcat 8.5.40
  • mysql 5.7

所用技術(shù):

  •  jsp+servlet
  • js+ajax
  • layUi
  • jdbc直連

項(xiàng)目訪問地址

http://localhost:8090

項(xiàng)目結(jié)構(gòu)

項(xiàng)目截圖

注冊(cè)

我的網(wǎng)盤

我的共享

回收站

會(huì)員充值

管理員-所有文件

管理員-共享申請(qǐng)

關(guān)鍵代碼:

1.初始化工作

//數(shù)據(jù)庫(kù)連接初始化
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啟動(dòng)時(shí) ,讀取配置文件中關(guān)于上傳的信息
  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");
    //配置上傳臨時(shí)目錄
    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">&times;</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">
              關(guān)閉
            </button>
            <input type="submit" class="btn btn-primary" value="確定上傳"/>
          </div>
        </div>
    </form>
  </div>
//后端入庫(kù)處理
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);
          //上傳成功,數(shù)據(jù)庫(kù)保存記錄
          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("上傳出錯(cuò)");
        }
      }
    }
  } catch (FileUploadException e) {
    e.printStackTrace();
    response.setContentType("text/html; charset=utf8");
    response.getWriter().print("上傳出錯(cuò)!!");
  }
}

3.檢索重復(fù)上傳的資源

//這里上傳在上面上傳資源時(shí)候,將保存原始資源名字
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.平臺(tái)會(huì)員充值

//前端jsp代碼
<body>
  <div id="wrapper">
    <%@include file="common/nav.jsp" %>
    <div id="page-wrapper">
      <div class="">
    <div class="col-md-12">
      <h1 class="margin-bottom-15">以下是微信付款碼,掃碼即可支付</h1>
      <div class="form-group">
        <div class="col-md-12">
          <div class="control-wrapper">
            <div class="text-center" id="div1">
              <input type="submit" value="掃碼付款" class="btn btn-success">
            </div>
            <div class="text-center" id="div2" style="display: none;">
              <input type="submit" value="恭喜您,完成掃碼支付!"class="btn btn-warning">
            </div>
          </div>
          <div class="text-center">
            <font color="green">${msgSuccess }</font>
            <font color="red">${msgFail }</font>
          </div>
        </div>
      </div>
      <form class="form-horizontal templatemo-container templatemo-login-form-1 margin-bottom-30" role="form" action="user" method="post">
        <input type="hidden" name="action" value="editSubmit">
        <div class="form-group">
         <img src="static/image/pay.png" alt="平臺(tái)會(huì)員充值碼" style="width: 81%; padding-left: 24%;">
        </div>
       </form>
    </div>
  </div>
    </div>
  </div>
</body>
//js實(shí)現(xiàn),采用定時(shí)跳轉(zhuǎn)模擬真實(shí)用戶支付流程,后續(xù)進(jìn)行改動(dòng)用戶會(huì)員狀態(tài)
var test1 = setTimeout(function(){
  $("#div1").css("display","none");
  $("#div2").css("display","block");
  layer.msg('恭喜您,完成掃碼支付!', {time: 4000, icon:6},function () {
    window.location.href="user?action=doTopUp";
  });
  clearTimeout(test1);
},5000);
//后端代碼
public void doTopUp(User user) {
  Connection conn = db.getConnection();
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
    //members=1為會(huì)員狀態(tài)
    ps = conn.prepareStatement("update user set members = 1 where id = ?");
    ps.setInt(1, user.getId());
    ps.execute();
  } catch (SQLException e) {
    e.printStackTrace();
  } finally {
    try {
      if (conn != null)
        conn.close();
      if (ps != null)
        ps.close();
      if (rs != null)
        rs.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

項(xiàng)目后續(xù)

其他ssh,ssm,springboot版本后續(xù)迭代更新,持續(xù)關(guān)注

到此這篇關(guān)于手把手搭建Java共享網(wǎng)盤的方法步驟的文章就介紹到這了,更多相關(guān)Java搭建共享網(wǎng)盤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java中concat()方法的使用說明

    java中concat()方法的使用說明

    這篇文章主要介紹了java中concat()方法的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java深入淺出數(shù)組的定義與使用上篇

    Java深入淺出數(shù)組的定義與使用上篇

    數(shù)組是有序的元素序列,若將有限個(gè)類型相同的變量的集合命名,那么這個(gè)名稱為數(shù)組名。組成數(shù)組的各個(gè)變量稱為數(shù)組的分量,也稱為數(shù)組的元素,有時(shí)也稱為下標(biāo)變量。數(shù)組是在程序設(shè)計(jì)中,為了處理方便, 把具有相同類型的若干元素按有序的形式組織起來的一種形式
    2022-03-03
  • Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理

    Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理

    這篇文章主要為大家介紹了Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    這篇文章主要介紹了SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java日志軟件Log4j的基本使用教程

    Java日志軟件Log4j的基本使用教程

    這篇文章主要介紹了Java日志軟件Log4j的基本使用教程,包括回滾和發(fā)送日志郵件等基本功能使用的講解,需要的朋友可以參考下
    2015-12-12
  • SWT JFace 拖曳效果

    SWT JFace 拖曳效果

    SWT(JFace)體驗(yàn)之拖曳效果
    2009-06-06
  • JVM中的程序計(jì)數(shù)寄存器PC是什么詳解

    JVM中的程序計(jì)數(shù)寄存器PC是什么詳解

    這篇文章主要介紹了JVM中的程序計(jì)數(shù)寄存器PC原理分析,JVM中的程序計(jì)數(shù)寄存器(Program Counter Register),Register的命名源于CPU的寄存器,寄存器存儲(chǔ)指令相關(guān)的現(xiàn)場(chǎng)信息
    2021-09-09
  • JCrontab簡(jiǎn)單入門實(shí)例詳解

    JCrontab簡(jiǎn)單入門實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了JCrontab簡(jiǎn)單入門實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • spring security中的默認(rèn)登錄頁(yè)源碼跟蹤

    spring security中的默認(rèn)登錄頁(yè)源碼跟蹤

    原來Spring Security有一個(gè)默認(rèn)的WebSecurityConfigurerAdapter,發(fā)現(xiàn)其中有一個(gè)init方法,于是在這個(gè)方法打了斷點(diǎn),在應(yīng)用啟動(dòng)的時(shí)候進(jìn)行跟蹤,這篇文章主要介紹了spring security之 默認(rèn)登錄頁(yè)源碼跟蹤,需要的朋友可以參考下
    2021-11-11
  • Request與Session的存值取值操作

    Request與Session的存值取值操作

    這篇文章主要介紹了Request與Session的存值取值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論