第一种方法 以下载方式在JSP中显示本地图片。
jsp无法加载本地磁盘下的图片,这时可以通过下载形式将图片下载下来传到jsp界面进行展示。
struts2 中 java代码编写 使用自带工具类 。
public class showImg extends ActionSupport{
/**
* 给前台界面展示图片 使用工具类
* @return
*/
public String downLoad2() {
File file = new File("E:\\images\\gug.jpg");
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("multipart/form-data");
//使用了struts2的文件复制工具类。
FileUtils.copyFile(file, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//返回流对象,经过struts.xml进行处理。
public InputStream getInputStream() {
File file = new File("E:\\images\\gug.jpg");
FileInputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return is;
}
}