網站首頁 編程語言 正文
?文件的下載
獲得服務器文件的根路徑
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
相關推薦
- 2022-12-09 python反射機制內置函數及場景構造詳解_python
- 2022-05-02 Numpy中創建數組的9種方式小結_python
- 2023-07-17 uniapp H5頁面內獲取手機號撥打電話
- 2022-06-14 C#使用正則表達式_C#教程
- 2022-08-07 Android?Gradle?插件自定義Plugin實現注意事項_Android
- 2022-10-11 云服務器搭建redis主從復制以及哨兵模式(附踩坑記錄)
- 2022-08-17 React自定義hook的方法_React
- 2022-06-30 詳解Python如何實現尾遞歸優化_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支