java --多文件上傳2

配置部分參考java --多文件上傳1
html

直接貼出代碼java

1.controller層
瀏覽器

    /**
     * 
     * 測試文件上傳
     * 
     * **/
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public ResultObject test(MultipartHttpServletRequest request) {

        System.out.println("request:" + request);
        ResultObject ro = customerCommentService.test(request);
        return ro;
    }

2.service層
app

    /**
     * 測試文件上傳
     * 
     * **/
    public ResultObject test(MultipartHttpServletRequest request);

3.service實現層
post

    /**
     * 
     * 測試文件上傳
     * 
     * **/
    public ResultObject test(MultipartHttpServletRequest request){
        
        ResultObject ro = new ResultObject();
        
        //接收全部的文件
        List<MultipartFile> file = request.getFiles("files");
        System.out.println("取到的文件個數:"+file.size());
        //獲取系統路徑
        String ctxPath=request.getSession().getServletContext().getRealPath("/")+"Upload\\";
        System.out.println("文件路徑:"+ctxPath);
        
        //若是目錄不存在,則新建
        File dir = new File(ctxPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //建立文件輸出流
        FileOutputStream fileOutputStream = null;
        
        //記錄上傳過程起始時的時間,用來計算上傳時間
        int pre = (int) System.currentTimeMillis();
        for (int i = 0; i < file.size(); i++) 
        {
            if (!file.get(i).isEmpty()) 
            { 
              //得到文件名字
              String fileName =  file.get(i).getOriginalFilename();
              System.out.println("上傳的文件名:"+fileName);
              try{
                  //拿到輸出流
                  fileOutputStream = new FileOutputStream(ctxPath+fileName);
                  //
                  fileOutputStream.write(file.get(i).getBytes());
                  //
                  fileOutputStream.flush(); 
              }catch(Exception e)
                 {
                  e.printStackTrace();
                    //上傳失敗
                 }
              if (fileOutputStream != null) {
                  try { 
                  //關閉流
                  fileOutputStream.close();
                  //上傳成功
                  
                  }catch(Exception e)
                  {
                      e.printStackTrace();
                      //上傳失敗
                  }
              }
            }
        }
        //記錄上傳該文件後的時間 
        int finaltime = (int) System.currentTimeMillis();
        System.out.println(finaltime - pre); 
        
        return ro;
    }

4.測試代碼測試

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
    <title> This is my HTML </title>
</head>
<body>
    <h3> 上傳多文件實例 </h3>

    <form action="http://192.168.2.67:8080/pets/test" method="post" enctype="multipart/form-data">
        選擇文件1:<input type="file" name="files"><br>
        選擇文件2:<input type="file" name="files"><br>
        <input type="submit" value="提交">
    </form>

</body>
</html>

5.瀏覽器請求ui

注意查看,選擇的文件名稱調試

6.斷點調試code

7.物理路徑查看文件orm



自此,結束。

相關文章
相關標籤/搜索