[英]How can I display image file object from java to jsp?
I have a method which returns a file object of an image: 我有一个返回图像文件对象的方法:
public File getPhoto(entryId){...}
I call this method from my action method and set the file to a DTO File variable: 我从操作方法中调用此方法,并将文件设置为DTO File变量:
myDto.photo = getPhoto(entryId);
// where entryId refers to the name of the image file
// e.g. ent01 for ent01.gif, ent02 for ent02.gif and so on.
Now, in my JSP file I would like to display the image through a code like this: 现在,在我的JSP文件中,我想通过如下代码显示图像:
<img src = "${myDto.photo}">
However,I realized that the myDto.photo is a file object thus has the absolute path of the file and not the URL needed for the img src in JSP. 但是,我意识到myDto.photo是文件对象,因此具有文件的绝对路径,而不是JSP中img src所需的URL。
Through searching, I understand that I can use a servlet and use something like 通过搜索,我了解可以使用servlet并使用类似
<img src = "${pageContext.request.contextPath}/image/ent01.gif"}.
However, I'm a little confused about this one as I wanted the filename part (ent01.gif) to vary based from the input entryId. 但是,我对此感到有些困惑,因为我希望文件名部分(ent01.gif)与输入entryId不同。
I hope anyone can shed light for me on this one. 我希望任何人都可以为我阐明这一点。 A lot of thanks. 非常感谢。
You can Create a Controller Class for you to diplay the image you want. 您可以创建一个控制器类来显示所需的图像。
@Controller
public class ImageReadFile{
// this is for mapping your image related path.
@RequestMapping(value="/image/*")
public void readImage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = request.getServletContext();
//here i uploaded my image in this path
// You can set any path here
String imagePath = "/home/somefolder/Workspaces/Images/";
String [] fragmentFilename = request.getServletPath().split("/");
//Check if image isn't set
if(fragmentFilename.length <= 2){
return;
}
String filename = fragmentFilename[2];
String requestedImage = "/"+filename;
if(filename == null){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
if(!image.exists()){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String contentType = sc.getMimeType(image.getName());
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
Files.copy(image.toPath(), response.getOutputStream());
}
}
Servlet Version. Servlet版本。
try this. 尝试这个。
@WebServlet("/image/*")
public class ImageWriter extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = request.getServletContext();
//here i uploaded my image in this path
// You can set any path here
String imagePath = "/home/somefolder/Workspaces/Images/";
String [] fragmentFilename = request.getServletPath().split("/");
//Check if image isn't set
if(fragmentFilename.length <= 2){
return;
}
String filename = fragmentFilename[2];
String requestedImage = "/"+filename;
if(filename == null){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
if(!image.exists()){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String contentType = sc.getMimeType(image.getName());
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
Files.copy(image.toPath(), response.getOutputStream());
}
}
this is how you gonna set display it in jsp, 这就是您要在jsp中设置显示的方式,
<img alt="${imageFilename}" src="${pageContext.request.contextPath}/image/${imageFilename}">
Just pass the Filename to jsp then let the controller read it and display it. 只需将Filename传递给jsp,然后让控制器读取并显示它即可。
hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.