내 하드 드라이브에 저장된 이미지를 어떻게 확보할 수 있는 < br/> 서브렛?;; Example:< 대한 br/>; 나는, & # 39 에 저장된 이미지를 삼중평균 < code> Images/button.png& < /code> # 39,,, 보고 싶습니다;;;; /code> file/button.png< URL 이 인할지 경배하되 서브렛에 < code>.
다음은 apc® 코드:
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletContext cntx= req.getServletContext();
// Get the absolute path of the image
String filename = cntx.getRealPath("Images/button.png");
// retrieve mimeType dynamically
String mime = cntx.getMimeType(filename);
if (mime == null) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
resp.setContentType(mime);
File file = new File(filename);
resp.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
out.close();
in.close();
}