日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring mvc 实现单文件 || 多文件上传

發(fā)布時間:2023/12/10 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring mvc 实现单文件 || 多文件上传 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文件上傳

    • 1. pom依賴(jar包)
    • 2. 文件上傳解析器配置
    • 3. 上傳實現(xiàn)
    • 4. 下載||文件展示實現(xiàn)(io流的實現(xiàn))

項目下載地址
https://github.com/sevenyoungairye/File-Upload

1. pom依賴(jar包)

<!-- common upload file --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!-- common upload io--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>

2. 文件上傳解析器配置

spring-mvc.xml

<!-- 文件上傳解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--指定總文件大小 單位: b--><property name="maxUploadSize" value="10000000"/><!--指定單個文件的大小--><property name="maxUploadSizePerFile" value="2000000"/><!--指定編碼--><property name="defaultEncoding" value="utf-8"/></bean>

3. 上傳實現(xiàn)

  • 頁面展示
<%--Created by IntelliJ IDEA.User: echo lovelyDate: 2020/9/5Time: 19:37文件上傳測試 demo --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>File Upload Demo..</title> </head> <body><form method="post" enctype="multipart/form-data" action="fileUpload1">name: <input type="text" name="name" /> <br/>file: <input type="file" name="fileUpload" /> <br/><input type="submit" value="upload"></form><br/>多文件上傳:<form method="post" enctype="multipart/form-data" action="fileUpload2">file1 <input type="file" name="uploadFiles"> <br/>file2 <input type="file" name="uploadFiles"> <br/><input type="submit" value="upload"></form></body> </html>
  • controller接收文件實現(xiàn)
package com.bitqian.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException; import java.util.UUID;/** * spring mvc 測試文件上傳 * @author echo lovely * @date 2020/9/5 9:21 */ @Controller public class FileUploadDemo {@RequestMapping(value = "/fileUpload1")@ResponseBodypublic void upload1(String name, MultipartFile fileUpload) {System.out.println(name);System.out.println(fileUpload);String originalFilename = fileUpload.getOriginalFilename();try {// 將文件保存到文件夾//fileUpload.transferTo(new File("f://Temp//" + originalFilename));String ext = originalFilename.substring(originalFilename.lastIndexOf("."));fileUpload.transferTo(new File("f://Temp//" + UUID.randomUUID() + ext));} catch (IOException e) {e.printStackTrace();}}// 多文件上傳..@RequestMapping(value = "/fileUpload2")@ResponseBodypublic void uploadFile2(MultipartFile[] uploadFiles) {String path = "f://temp//";File file = new File(path);// 文件夾不存在創(chuàng)建temp文件夾if (!file.exists())file.mkdirs();if (uploadFiles != null) {for (MultipartFile uploadFile : uploadFiles) {String originalFilename = uploadFile.getOriginalFilename();System.out.println("源文件名:" + originalFilename);// uuid + 文件后綴名String fileName = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));try {// 上傳文件到指定目錄uploadFile.transferTo(new File(path + fileName));} catch (IOException e) {e.printStackTrace();}}}}}

4. 下載||文件展示實現(xiàn)(io流的實現(xiàn))

  • 文件下載
@RequestMapping(value="/download/{id}") public void downloader(HttpServletResponse resp, @PathVariable(value = "id") int stuId) {StudentInfo stu = stuInfoService.queryOne(stuId);// 圖像路徑String imgPath = stu.getImgPath();if (imgPath == null)return;// 獲取圖像名的后綴String suffix = imgPath.substring(imgPath.lastIndexOf("."));// 設(shè)置文件 ContentType 自動判斷下載類型resp.setContentType("multipart/form-data");// 設(shè)置文件頭resp.setHeader("Content-Disposition", "attachment;fileName="+UUID.randomUUID() + suffix);FileInputStream fis = null;ServletOutputStream outputStream = null;try {fis = new FileInputStream(new File(imgPath));// 使用流輸出到客戶端outputStream = resp.getOutputStream();byte[] b = new byte[1024];// 讀取到數(shù)組里面int read = fis.read(b);while(read != -1) {outputStream.write(b, 0, read);read = fis.read(b);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (outputStream != null)outputStream.close();if (fis != null)fis.close();} catch (IOException e) {e.printStackTrace();}}}
  • 文件展示到頁面
/*** 圖片展示* @throws IOException */ @RequestMapping("/shwoImg/{id}") public void shwoImg(@PathVariable(value = "id") int stuId, HttpServletResponse resp) {StudentInfo stu = stuInfoService.queryOne(stuId);// 從數(shù)據(jù)庫中取到圖片路徑String imgPath = stu.getImgPath();if (imgPath == null)return;// 設(shè)置文件展示的類型resp.setContentType("image/jpeg");// 拿到out流ServletOutputStream out = null;// 文件讀取FileInputStream fis = null;try {out = resp.getOutputStream();// 讀取到文件fis = new FileInputStream(new File(imgPath));// 使用一個byte數(shù)組byte[] b = new byte[1024];// 將讀取的字節(jié)裝入byte數(shù)組int read = fis.read(b);while (read != -1) {out.write(b, 0, read);read = fis.read(b);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null)fis.close();} catch (IOException e) {e.printStackTrace();}try {if (out != null) {out.flush();out.close();}} catch (IOException e) {e.printStackTrace();}}} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的spring mvc 实现单文件 || 多文件上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。