獲取路徑/image
由
getServletContext().getRealPath("/images");
存儲&讀取文件,即使你可以讀從外部位置的文件從你的servlet
File image = new File("d:\\image\1.jpg");
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
// Gently close streams.
close(output);
close(input);
}
請參見
可以說,我的形象是在d:/ images文件夾和我在同一臺機器上運行Jetty服務器。我如何訪問我的本地文件夾(D:/ images)? – ghTvNath