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;
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis/Mybatis-Plus駝峰式命名映射的實現(xiàn)
本文主要介紹了Mybatis-Plus駝峰式命名映射的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
SpringBoot整合easyExcel實現(xiàn)CSV格式文件的導入導出
這篇文章主要為大家詳細介紹了SpringBoot整合easyExcel實現(xiàn)CSV格式文件的導入導出,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以參考下2024-02-02
方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Java線程安全的計數(shù)器簡單實現(xiàn)代碼示例
這篇文章主要介紹了Java線程安全的計數(shù)器簡單實現(xiàn)代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-10-10
Mybatis動態(tài)SQL?foreach批量操作方法
這篇文章主要介紹了Mybatis動態(tài)SQL?foreach批量操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

