專案釋出後能訪問到本地的檔案(圖片) (java web專案中讀取本地圖片)
阿新 • • 發佈:2019-02-19
在專案中,因為業務需要,使用者上傳的圖片存放在伺服器的D盤中,為了讀取並顯示到頁面上,嘗試了兩種方法:
一 通過流讀取
java程式碼:
- @RequestMapping(value = "/seekExperts")
- @ResponseBody
- public String createFolw(HttpServletRequest request,
- HttpServletResponse response, Model model) {
- // response.setContentType("image/*");
- PageData pd = new
- pd = this.getPageData();
- //取路徑
- String path = pd.getString("path");
- FileInputStream fis = null;
- OutputStream os = null;
- try {
- fis = new FileInputStream(path);
- os = response.getOutputStream();
- int count =
- byte[] buffer = newbyte[1024 * 8];
- while ((count = fis.read(buffer)) != -1) {
- os.write(buffer, 0, count);
- os.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- try
- fis.close();
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return"ok";
- }
前臺程式碼:
- <imgalt="image"id="myImage"style="height:250px;width:400px;"src="defectivemanage/seekExperts.do?path=${var.PATH }"/>
二 通過配置虛擬目錄讀取
開啟tomcat的conf資料夾,在server.xml中的<Host></Host>標籤內加入
- <Contextpath="/dataResourceImages"docBase="D:\a"crossContext="true"reloadable="false"debug="0"/>
jsp程式碼:
- <imgalt="image"id="myImage"style="height:250px;width:400px;"src="/dataResourceImages/GIF1.gif"/>
修改後重啟,就可以看到圖片啦