讀取服務器端的文件流

 HttpServletResponse response = ServletActionContext.getResponse();
   //文件的路徑

  String contextPath = ServletActionContext.getServletContext()
    .getRealPath("/")+ "userimages\\";
   InputStream iStream = null;
   
   iStream = new FileInputStream(new File(contextPath+photoSavePath));
   
   response.setHeader("Content-Type", "image/png");
 
   OutputStream os = null;
   
   os = response.getOutputStream();
   
   byte[] cache = new byte[1024];
   
   int len = -1;
 
   while ((len = iStream.read(cache)) != -1) {
    os.write(cache, 0, len);
   }
 
   os.flush();

發佈了38 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章