1. 程式人生 > >單個檔案上傳+多個檔案上傳

單個檔案上傳+多個檔案上傳

單個檔案上傳

  1. jsp頁面
   <body>
   <form action="shangchuan.do"  method="post" enctype="multipart/form-data">
        檔案<input type="file" name="fileName"> //name與方法引數保持一致
       <input type="submit" value="上傳">
    </form>
  </body>`

2.檔案上傳的檢視解析器

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

3.編寫Controller

package com.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework
.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller public class uploadController { @RequestMapping("shangchuan.do") public String upload(@RequestParam(value="fileName")MultipartFile fileName,HttpServletRequest request) throws IllegalStateException, IOException{ //獲取上傳檔案的 原始名稱:123
+時間戳.jpg String fname=fileName.getOriginalFilename(); fname=System.currentTimeMillis()+fname;//時間戳 System.out.println("原始名:"+fname); System.out.println("臨時名:"+fileName.getName()); //獲取伺服器上的upload資料夾的物理位置 :e:\tomcat\...\... String path=request.getSession().getServletContext().getRealPath("/upload"); System.out.println("upload資料夾位置:"+path); File target=new File(path+"\\"+fname); fileName.transferTo(target);////儲存檔案到指定目錄 return "success.jsp"; }

4.資料夾:伺服器的資料夾
上傳後的資訊最終會放到伺服器上的upload資料夾中
這裡寫圖片描述
5.結果:
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

多個檔案上傳

  1. jsp頁面
   <body>
   <form action="duoshangchuan.do"  method="post" enctype="multipart/form-data">
        <input type="file" name="files"> <br>
        <input type="file" name="files"> <br>
        <input type="file" name="files"> <br>
       <input type="submit" value="都上傳">
    </form>
  </body>`

2.檔案上傳的檢視解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

3.編寫Controller

package com.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class uploadController {
    @RequestMapping("duoshangchuan.do")
    public String duoshangchuan(@RequestParam(value="files")MultipartFile[] files,HttpServletRequest request) throws IllegalStateException, IOException{

        if(files!=null&&files.length>0){
          for(int i=0;i<files.length;i++){
              MultipartFile file=files[i];
              String fname=file.getOriginalFilename();
                     fname=System.currentTimeMillis()+fname;
              String path=request.getSession().getServletContext().getRealPath("/upload");
              System.out.println("路徑:"+path+"名:"+fname);
              File target=new File(path+"/"+fname);
               file.transferTo(target);
          }
        }
        return "success.jsp";
    }
}

4.資料夾:伺服器的資料夾
上傳後的資訊最終會放到伺服器上的upload資料夾中
這裡寫圖片描述
5.結果:
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述