1. 程式人生 > 其它 >SpringMVC實現檔案上傳和下載

SpringMVC實現檔案上傳和下載

ResponseEntity實現檔案下載

使用ResponseEntity實現下載檔案的功能

package com.lalala.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileUploadAndDownloadController {
    @RequestMapping("/testDownload")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
        //獲取ServletContext物件
        ServletContext servletContext = session.getServletContext();
        //獲取伺服器中檔案的真實路徑
        String realPath = servletContext.getRealPath("/static/img/1.jpg");
        //建立輸入流
        InputStream is = new FileInputStream(realPath);
        //建立位元組陣列
        byte[] bytes = new byte[is.available()];
        //將流讀到位元組陣列中
        is.read(bytes);
        //建立HttpHeaders物件設定響應頭資訊
        MultiValueMap<String, String> headers = new HttpHeaders();
        //設定要下載方式以及下載檔案的名字
        headers.add("Content-Disposition", "attachment;filename=1.jpg");
        //設定響應狀態碼
        HttpStatus statusCode = HttpStatus.OK;
        //建立ResponseEntity物件
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
        //關閉輸入流
        is.close();
        return responseEntity;
    }
}

實現檔案上傳

新增依賴:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

在springmvc.xml中配置檔案上傳解析器:

<!-- 配置檔案上傳解析器,將上傳的檔案封裝為MultipartFile -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

controller如下:

package com.lalala.controller;

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

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileUploadAndDownloadController {
    @RequestMapping("/testUpload")
    public String testUp(MultipartFile photo, HttpSession session) throws IOException {
        //獲取上傳的檔案的檔名
        String fileName = photo.getOriginalFilename();
        //獲取上傳檔案的字尾名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //將UUID作為檔名
        fileName = UUID.randomUUID().toString() + suffixName;
        //獲取伺服器中photo目錄的路徑
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("photo");
        File file = new File(photoPath);
        if(!file.exists()){
            file.mkdir();
        }
        String finalPath = photoPath + File.separator + fileName;
        //實現上傳功能
        photo.transferTo(new File(finalPath));
        return "success";
    }
}