java讀取本地圖片和下載可選擇路徑儲存圖片

來源:互聯網
上載者:User
   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();}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.