1. 讀取本地圖片 前台 <img src="<%=path%>xx! showPicture " />
/** * 讀取本地圖片,並顯示 */public void showPicture(){LOG.info("<==StudentHisAction.toSumSetUser==>");super.setHttpServlet();if(StringUtils.isNotEmpty(picPath)){picPath =picPath.replace("/", File.separator);FileInputStream is = null;try {is = new FileInputStream(picPath);int size = is.available(); //檔案大小byte[] data = new byte[size];is.read(data);is.close();super.getResponse().setContentType("image/*");OutputStream os = super.getResponse().getOutputStream(); //響應輸出資料流os.write(data);os.close();} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}}
2.下載可選擇路徑儲存圖片(伺服器)
/** * 用途:下載伺服器圖片 */public void downPic(){LOG.info("<==StudentHisAction.downPic==>");super.setHttpServlet(); InputStream fis = null; OutputStream toClient = null; try { if(!StringUtils.isNotEmpty(picPath)) return; // path是指欲下載的檔案的路徑。 File file = new File(picPath); // 取得檔案名稱。 String filename = file.getName(); // 以流的形式下載檔案。 fis = new BufferedInputStream(new FileInputStream(picPath)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); // fis.close(); // 清空response super.getResponse().reset(); // 設定response的Header super.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); super.getResponse().addHeader("Content-Length", "" + file.length()); toClient = new BufferedOutputStream(response.getOutputStream()); super.getResponse().setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); //toClient.close(); } catch (IOException ex) { ex.printStackTrace(); }finally{ if( fis !=null){ try{ fis.close(); toClient.close(); }catch (IOException ex) { ex.printStackTrace(); } } }}
3.下載http形式圖片
/** * 下載伺服器圖片(http形式下載) */public void downPicByHttp(){LOG.info("<==StudentHisAction.downPicByHttp==>");super.setHttpServlet();if(StringUtils.isEmpty(picPath)) return;InputStream is = null;OutputStream os = null;HttpURLConnection httpURLConnection = null;try{picPath =picPath.replaceAll("\\\\", "/");File file = new File(picPath);String fileName = file.getName(); super.getResponse().reset(); // 清空responsesuper.getResponse().setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes())); // 設定response的Headersuper.getResponse().setContentType("application/octet-stream");URL url = new URL(picPath);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(5000);//設定網路連接逾時時間httpURLConnection.setDoInput(true);//設定應用程式要從網路連接讀取資料httpURLConnection.setRequestMethod("GET");//設定方式int responseCode = httpURLConnection.getResponseCode();//擷取相應codeif(responseCode==200){is = httpURLConnection.getInputStream();//從伺服器返回一個輸入資料流os = new java.io.BufferedOutputStream(super.getResponse().getOutputStream());byte[] buffer = new byte[httpURLConnection.getContentLength()];int len ;while((len=is.read(buffer,0, buffer.length))!=-1){os.write(buffer, 0, len);}}}catch(Exception e){e.printStackTrace();System.out.println("下載圖片失敗:"+e.getMessage());}finally{try {is.close();os.close();} catch (IOException e) {e.printStackTrace();}}}