web 文件上傳和下載

1,文件下載功能:
控制器層代碼如下:

/*下載測試*/
    @RequestMapping(value="downloadTest.do")
    public void downloadTest(HttpServletResponse response, HttpServletRequest request){
        log.info("下載測試, downloadTest.do,");
        String str =  "下載測試";
        ServletOutputStream sos = null;
        FileInputStream fis = null;
        try {
            sos = response.getOutputStream(); //得到網絡輸出流
            //獲取文件真實路徑
            String filePath = request.getSession().getServletContext().getRealPath("/web-inf/file/music/music1.mp3");
            String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); //獲取文件名
            response.setContentType("multipart/form-data"); //設置文件類型(自動)
            response.setHeader("Content-Disposition", "attachment;fileName="+ fileName); //設置文件名
            fis = new FileInputStream(filePath); //文件輸入流
            int len; //一次讀取文件字節長度
            byte[] bytes = new byte[1024]; //字節緩存區域
            while((len = fis.read(bytes)) != -1){
                sos.write(bytes, 0, len);
            }
            sos.flush(); //刷新輸出流(確保所有字節都已經通過)
        } catch (IOException e) {
            e.printStackTrace();
        } finally{ //關閉流
            try {
                sos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

頁面標籤:

<a class="btn btn-info" type="button" href="test/downloadTest.do">下載</a>

實現效果:
這裏寫圖片描述

二,文件上傳
前端標籤如下,

 <form enctype="multipart/form-data" action="test/upload.do" method="post">
                <div class="row">
                    <div class="col-xs-4">
                        <input type="file" name="file" >
                    </div>
                    <div class="col-xs-4">
                        <input type="submit" class="btn btn-danger" value="上傳文件">
                    </div>
                </div>
            </form>

注意: enctype=”multipart/form-data” 不能少
然後在 springmvc的配置文件中加入:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"/>
    </bean>

後臺使用MultipartFile 類接收上傳來的文件,

 /*上傳測試*/
    @RequestMapping(value="upload.do")
    public ModelAndView upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map<String, Object> json = new HashMap<String, Object>();
        FileOutputStream fos = null;
        try {
            byte[] bytes = file.getBytes();
            log.info("file-byte-lengerth: " + bytes.length);

            String fileName = request.getServletContext().getRealPath("/web-inf/file/" + file.getOriginalFilename());
            log.info("fileName: " + fileName);
            fos = new FileOutputStream(fileName);
            fos.write(bytes);
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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