Java實(shí)現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式
將圖片上傳到webapp路徑下 路徑獲取方式
此方法獲取到工程webapp文件夾下
String contexPath= request.getSession().getServletContext().getRealPath("/");
獲取IP地址端口號(hào)以及項(xiàng)目名稱
<% 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(); //設(shè)置響應(yīng)的報(bào)頭信息(中文問題解決辦法) 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("下載圖片出錯(cuò)"); if (input != null) { IOUtils.closeQuietly(input); } } return null; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis/Mybatis-Plus駝峰式命名映射的實(shí)現(xiàn)
本文主要介紹了Mybatis-Plus駝峰式命名映射的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以參考下2024-02-02方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java線程安全的計(jì)數(shù)器簡單實(shí)現(xiàn)代碼示例
這篇文章主要介紹了Java線程安全的計(jì)數(shù)器簡單實(shí)現(xiàn)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10Mybatis動(dòng)態(tài)SQL?foreach批量操作方法
這篇文章主要介紹了Mybatis動(dòng)態(tài)SQL?foreach批量操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03java發(fā)送get請(qǐng)求和post請(qǐng)求示例
這篇文章主要介紹了java發(fā)送get請(qǐng)求和post請(qǐng)求示例,需要的朋友可以參考下2014-03-03