Java實現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式
更新時間:2021年11月12日 11:26:40 作者:Architect_csdn
這篇文章主要介紹了Java實現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
將圖片上傳到webapp路徑下 路徑獲取方式
此方法獲取到工程webapp文件夾下
String contexPath= request.getSession().getServletContext().getRealPath("/");
獲取IP地址端口號以及項目名稱
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
spring java 獲取webapp下文件路徑
@RequestMapping("/act/worldcup_schedule_time/imgdownload")
@ResponseBody
public String scheduleDownload(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
response.setCharacterEncoding("UTF-8");
String downLoadName = "worldcup.jpg";
InputStream input = null;
try {
request.setCharacterEncoding("UTF-8");
//獲取文件的路徑
// String url = session.getServletContext().getRealPath("/") + "resources\\images\\act\\worldcup_merge\\worldcup720.png";
String url = session.getServletContext().getRealPath("/") + "resources/images/act/worldcup_merge/worldcup720.png";
System.out.println(url);
File file = new File(url);
input = FileUtils.openInputStream(file);
byte[] data = IOUtils.toByteArray(input);
//System.out.println("文件名:"+downLoadName);
response.reset();
//設置響應的報頭信息(中文問題解決辦法)
response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(downLoadName, "UTF-8"));
response.addHeader("Content-Length", "" + data.length);
response.setContentType("image/png; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
} catch (Exception e) {
logger.error("下載圖片出錯");
if (input != null) {
IOUtils.closeQuietly(input);
}
}
return null;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 線程的優(yōu)先級(setPriority)案例詳解
這篇文章主要介紹了Java 線程的優(yōu)先級(setPriority)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
springboot單獨使用feign簡化接口調(diào)用方式
這篇文章主要介紹了springboot單獨使用feign簡化接口調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
使用TraceId在Spring Cloud中實現(xiàn)線上問題快速定位
在微服務架構(gòu)中,服務間的互相調(diào)用使得問題定位變得復雜,在此背景下,TraceId為我們提供了一個在復雜環(huán)境中追蹤請求路徑和定位問題的工具,本文不僅介紹TraceId的基本概念,還將結(jié)合真實場景,為您展示如何在Spring Cloud中應用它2023-09-09
SpringBoot整合resilience4j實現(xiàn)接口限流
最近在開發(fā)項目的時候,需要用到限流的功能,本文主要介紹了SpringBoot整合resilience4j實現(xiàn)接口限流,具有一定的參考價值,感興趣的可以了解一下2024-01-01

