SSM中文件上傳與下載

單文件上傳

在spring-mvc中配置文件上傳的解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--  默認編碼   -->
    <property name="defaultEncoding" value="utf-8" />
    <!--  文件大小最大值  -->
    <property name="maxUploadSize" value="10485760000" />
    <!-- 內存中的最大值   -->
    <property name="maxInMemorySize" value="40960" />
</bean>

編寫Controller文件:

@RequestMapping("upload")
    public ModelAndView uploadFile(MultipartFile uploadFile,HttpSession session){
        //獲取上傳文件名
        String filename = uploadFile.getOriginalFilename();
        //獲取WebRoot下的images文件夾的絕對路徑作爲前半部分路徑
        String leftPath = session.getServletContext().getRealPath("/images");
        //將文件的前半部分路徑與文件名拼接
        File file = new File(leftPath, filename);
        try {
            uploadFile.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

網頁代碼:

<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
   <h2>文件上傳</h2>
                文件:<input type="file" name="uploadFile"/><br/><br/>
      <input type="submit" value="上傳"/>
</form>

到這裏單文件上傳就已經完成了,下面就是最後效果
上傳後的結果

多文件上傳

多文件上傳即在原本上傳的基礎上,將傳入參數MultipartFile uploadFile修改爲MultipartFile[] uploadFile數組形式,在上傳時就可以遍歷這個數組來完成多文件上傳

@RequestMapping("upload")
    public ModelAndView uploadFile(MultipartFile[] uploadFile,HttpSession session){
        //獲取上傳文件名
        for (int i = 0; i < uploadFile.length; i++) {
            String filename = uploadFile[i].getOriginalFilename();
            //獲取WebRoot下的images文件夾的絕對路徑作爲前半部分路徑
            String leftPath = session.getServletContext().getRealPath("/images");
            //將文件的前半部分路徑與文件名拼接
            File file = new File(leftPath, filename);
            try {
                uploadFile[i].transferTo(file);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

jsp頁面

<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
   <h2>文件上傳</h2>
      文件1:<input type="file" name="uploadFile"/><br/>
      文件2:<input type="file" name="uploadFile"/><br/>
      文件3:<input type="file" name="uploadFile"/><br/>
   <input type="submit" value="上傳"/>
</form>

多文件上傳結果

文件下載

文件下載的一些內容可以去看看對請求頭的分析


@RequestMapping("/download")
    public ResponseEntity<byte[]> downFile() throws IOException{
        File file = new File("C:\\Users\\TU\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\SSM_beta1\\images\\456.png");
        HttpHeaders headers = new HttpHeaders();
        String filename = new String("helloWorld.png".getBytes("UTF-8"),"iso-8859-1");
        //設置文件名
        headers.setContentDispositionFormData("attachment", filename);
        //以文件下載的形式來輸出流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                headers, HttpStatus.CREATED);
    }


下載效果
下載效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章