日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

springMVC 文件上傳和下載

作者:仰望星空的快樂 更新時間: 2022-05-10 編程語言

?文件的下載

獲得服務器文件的根路徑

String realPath=servletContext.getRealPath("/");

realPath:

E:\SpringMVC\Springmvc-project1\Springmvc-demo2\target\Springmvc-demo2-1.0-SNAPSHOT\

獲得根路徑之后在跟路徑后面加上我想創建的層次名和文件名,可以直接這樣寫:

String realPath=servletContext.getRealPath("/static/img/1.jpg");

realPath:

E:\SpringMVC\Springmvc-project1\Springmvc-demo2\target\Springmvc-demo2-1.0-SNAPSHOT\static\img\1.jpg

@Controller
public class FileUpAndDown {
    @RequestMapping("/download")
    public ResponseEntity downloadfile(HttpSession session)throws IOException
    {
//        獲取servletcontext對象(就是application對象,通過它可以讀取配置文件,javaweb基礎)
        ServletContext servletContext=session.getServletContext();
//        獲得要下載文件在服務器中存放的真實地址
        String realPath=servletContext.getRealPath("/static/img/1.jpg");
//        獲得文件地址的輸入字節流
        InputStream is=new FileInputStream(realPath);
//        獲得輸入文件的字節流的長度  is.available()代表字節流的長度
        byte[] bytes=new byte[is.available()];
//      將輸入流中的數據放到字節數組中
//      bytes就是下載文件的字節流,就是響應體
        is.read(bytes);
//       創建HttpHeaders對象設置響應頭信息
        MultiValueMap headers=new HttpHeaders();
//        在multivaluemap對象中,設置下載方式(附件形式)            下載的文件名
        headers.add("Content-Disposition","attachment;filename=1.jpg");
//        設置響應狀態碼
        HttpStatus status=HttpStatus.OK;
//         創建ResponseEntity對象     bytes 文件的字節流也就是響應體,
//         header文件的下載方式,文件臨時名稱 status 下載狀態碼,ok表示成功200
        ResponseEntity responseEntity=new ResponseEntity<>(bytes,headers,status);
//        關閉輸入流
        is.close();
        return  responseEntity;
    }

文件的上傳

不能使用get請求,只能使用post請求


文件上傳要求form表單的請求方式必須為post,并且添加屬性enctype="multipart/form-data"
SpringMVC中將上傳的文件封裝到MultipartFile對象中,通過此對象可以獲取文件相關信息
上傳步驟:
a>添加依賴:


commons-fileupload
commons-fileupload
1.3.1


b>在SpringMVC的配置文件中添加配置:



c>控制器方法:

先把文件要存放在服務器的地址的絕對路徑找出來,之后直接 photo.transferTo(file);即可

 //先把文件要存放在服務器的地址的絕對路徑找出來,之后直接 photo.transferTo(file);即可
    @RequestMapping("/upload")
//    MultipartFile 對應 java中的File類型
    public String upanddown(MultipartFile photo, HttpSession session) {
        System.out.println("開始上傳文件");
        System.out.println("要上傳的文件的photo中是:" + photo);
//        獲得真實的文件名 filename:node.dll
        String filename = photo.getOriginalFilename();
//        獲得真實文件名的后綴  .html   .java
        String suffixName = filename.substring(filename.lastIndexOf("."));
//        隨機生成一個文件名
        String uuid = UUID.randomUUID().toString();
//        將隨機生成的文件名和后綴拼接,就可以獲得要存放的文件名,這樣就可以避免文件名重復的問題
        filename = uuid + suffixName;
        System.out.println("真實的文件名filename是" + filename);
//        獲得servletcontext對象
        ServletContext servletContext = session.getServletContext();
//        獲得文件上傳之后,放置該文件的文件夾在服務器的真實路徑  (就是文件夾的絕對路徑)
//  E:\SpringMVC\Springmvc-project1\Springmvc-demo4\target\Springmvc-demo4-1.0-SNAPSHOT\photo
        String photopath = servletContext.getRealPath("photo");
        File file = new File(photopath);//獲取該文件夾
        System.out.println("接收該文件的文件夾在服務器上的絕對路徑:" + photopath);
// 檢查該photopath路徑是否存在,不存在就創建該文件夾
        System.out.println("file" + file);
        if (!file.exists()) {
            file.mkdir();
        }
//        拼接出最后文件的路徑
//E:\SpringMVC\Springmvc-project1\Springmvc-demo4\target\Springmvc-demo4-1.0-SNAPSHOT\photo\f772f503-b660-4d13-afbf-e903d911fc7b.html
        String finalpath = file + File.separator + filename;
        try {
            photo.transferTo(new File(finalpath));//核心語句,將photo上傳到file文件中
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }


?

原文鏈接:https://blog.csdn.net/sharesb/article/details/124580431

欄目分類
最近更新