網(wǎng)站首頁 編程語言 正文
?文件的下載
獲得服務(wù)器文件的根路徑
String realPath=servletContext.getRealPath("/");
realPath:
E:\SpringMVC\Springmvc-project1\Springmvc-demo2\target\Springmvc-demo2-1.0-SNAPSHOT\
獲得根路徑之后在跟路徑后面加上我想創(chuàng)建的層次名和文件名,可以直接這樣寫:
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基礎(chǔ))
ServletContext servletContext=session.getServletContext();
// 獲得要下載文件在服務(wù)器中存放的真實(shí)地址
String realPath=servletContext.getRealPath("/static/img/1.jpg");
// 獲得文件地址的輸入字節(jié)流
InputStream is=new FileInputStream(realPath);
// 獲得輸入文件的字節(jié)流的長度 is.available()代表字節(jié)流的長度
byte[] bytes=new byte[is.available()];
// 將輸入流中的數(shù)據(jù)放到字節(jié)數(shù)組中
// bytes就是下載文件的字節(jié)流,就是響應(yīng)體
is.read(bytes);
// 創(chuàng)建HttpHeaders對象設(shè)置響應(yīng)頭信息
MultiValueMap headers=new HttpHeaders();
// 在multivaluemap對象中,設(shè)置下載方式(附件形式) 下載的文件名
headers.add("Content-Disposition","attachment;filename=1.jpg");
// 設(shè)置響應(yīng)狀態(tài)碼
HttpStatus status=HttpStatus.OK;
// 創(chuàng)建ResponseEntity對象 bytes 文件的字節(jié)流也就是響應(yīng)體,
// header文件的下載方式,文件臨時名稱 status 下載狀態(tài)碼,ok表示成功200
ResponseEntity responseEntity=new ResponseEntity<>(bytes,headers,status);
// 關(guān)閉輸入流
is.close();
return responseEntity;
}
文件的上傳
不能使用get請求,只能使用post請求
文件上傳要求form表單的請求方式必須為post,并且添加屬性enctype="multipart/form-data"
SpringMVC中將上傳的文件封裝到MultipartFile對象中,通過此對象可以獲取文件相關(guān)信息
上傳步驟:
a>添加依賴:
commons-fileupload
commons-fileupload
1.3.1
b>在SpringMVC的配置文件中添加配置:
c>控制器方法:
先把文件要存放在服務(wù)器的地址的絕對路徑找出來,之后直接 photo.transferTo(file);即可
//先把文件要存放在服務(wù)器的地址的絕對路徑找出來,之后直接 photo.transferTo(file);即可
@RequestMapping("/upload")
// MultipartFile 對應(yīng) java中的File類型
public String upanddown(MultipartFile photo, HttpSession session) {
System.out.println("開始上傳文件");
System.out.println("要上傳的文件的photo中是:" + photo);
// 獲得真實(shí)的文件名 filename:node.dll
String filename = photo.getOriginalFilename();
// 獲得真實(shí)文件名的后綴 .html .java
String suffixName = filename.substring(filename.lastIndexOf("."));
// 隨機(jī)生成一個文件名
String uuid = UUID.randomUUID().toString();
// 將隨機(jī)生成的文件名和后綴拼接,就可以獲得要存放的文件名,這樣就可以避免文件名重復(fù)的問題
filename = uuid + suffixName;
System.out.println("真實(shí)的文件名filename是" + filename);
// 獲得servletcontext對象
ServletContext servletContext = session.getServletContext();
// 獲得文件上傳之后,放置該文件的文件夾在服務(wù)器的真實(shí)路徑 (就是文件夾的絕對路徑)
// 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("接收該文件的文件夾在服務(wù)器上的絕對路徑:" + photopath);
// 檢查該photopath路徑是否存在,不存在就創(chuàng)建該文件夾
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
相關(guān)推薦
- 2022-05-05 深入講解下Rust模塊使用方式_相關(guān)技巧
- 2022-10-21 Go語言使用goroutine及通道實(shí)現(xiàn)并發(fā)詳解_Golang
- 2022-07-06 QT5實(shí)現(xiàn)簡單的TCP通信的實(shí)現(xiàn)_C 語言
- 2022-04-03 Python-Selenium自動化爬蟲_python
- 2022-03-13 .NET中IoC框架Autofac用法講解_自學(xué)過程
- 2022-06-15 C++詳細(xì)講解繼承與虛繼承實(shí)現(xiàn)_C 語言
- 2022-05-26 Python編程中內(nèi)置的NotImplemented類型的用法_python
- 2022-09-20 Redis超詳細(xì)分析分布式鎖_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支