這篇文章將為大家詳細講解有關Java如何實現將圖片上傳到webapp路徑下,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
此方法獲取到工程webapp文件夾下
String contexPath= request.getSession().getServletContext().getRealPath("/");
獲取IP地址端口號以及項目名稱
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
@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; }
關于Java如何實現將圖片上傳到webapp路徑下就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。