Java 在servlet的jsp页面中显示图像
我的代码正在上载一个图像并将其保存到服务器,但我需要在jsp页面中显示该图像 用于上传图像的jsp uploadImage.jspJava 在servlet的jsp页面中显示图像,java,jsp,servlets,Java,Jsp,Servlets,我的代码正在上载一个图像并将其保存到服务器,但我需要在jsp页面中显示该图像 用于上传图像的jsp uploadImage.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Image Upload</title></head>
<body>
<form action="UploadImage" method="post" enctype="multipart/form-data"
name="productForm" id="productForm"><br><br>
<table width="400px" align="center" border=0 style="background-color:ffeeff;">
<tr>
<td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
Image Details</td>
</tr>
<tr>
<td align="center" colspan=2> </td>
</tr>
<tr>
<td>Image Link: </td>
<td>
<input type="file" name="file" id="file">
<td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>
</form>
</body>
</html>
图像上传
图像细节
图像链接:
Servlet
上传图像
public class UploadImage extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2082405235440894340L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File filenameImg;
List<FileItem> items = null;
try {
items = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(request);
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form fields here the same way as
// request.getParameter().
// You can get parameter name by
item.getFieldName();
// You can get parameter value by item.getString();
} else {
try{
// Process uploaded fields here.
String filename = FilenameUtils.getName(item.getName());
// Get filename.
String path = GetWebApplicationPathServlet.getContext().getRealPath("/images");
File file = new File(path,filename);
//File file = new File("C:\\", filename);
// Define destination file.
item.write(file);
System.out.println("filename: "+filename);
System.out.println("file: "+file);
request.setAttribute("image", file);
filenameImg = file;
// Write to destination file.
// request.setAttribute("image", filename);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Show result page.
System.out.println("request"+request.getAttribute("image"));
response.setContentType("image/jpeg");
//request.getRequestDispatcher("result.jsp").forward(request, response);
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}
}
公共类UploadImage扩展了HttpServlet{
/**
*
*/
私有静态最终长serialVersionUID=2082405235440894340L;
public void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
文件名img;
列表项=空;
试一试{
items=新的ServletFileUpload(新的DiskFileItemFactory())
.请求(request);
}捕获(文件上载异常){
抛出新的ServletException(“无法解析多部分请求。”,e);
}
用于(文件项:项){
if(item.isFormField()){
//在此处处理常规表单字段的方法与
//request.getParameter()。
//您可以通过以下方式获取参数名称:
item.getFieldName();
//您可以通过item.getString()获取参数值;
}否则{
试一试{
//在此处处理上载的字段。
字符串filename=FilenameUtils.getName(item.getName());
//获取文件名。
字符串路径=GetWebApplicationPathServlet.getContext().getRealPath(“/images”);
文件=新文件(路径、文件名);
//File File=新文件(“C:\\”,文件名);
//定义目标文件。
项目。写入(文件);
System.out.println(“文件名:“+filename”);
System.out.println(“文件:”+文件);
setAttribute(“图像”,文件);
filenameImg=file;
//写入目标文件。
//setAttribute(“图像”,文件名);
}
捕获(例外e){
e、 printStackTrace();
}
}
}
//显示结果页面。
System.out.println(“request”+request.getAttribute(“image”);
response.setContentType(“图像/jpeg”);
//getRequestDispatcher(“result.jsp”).forward(请求、响应);
字符串nextJSP=“/result.jsp”;
RequestDispatcher dispatcher=getServletContext()
.getRequestDispatcher(nextJSP);
转发(请求、响应);
}
}
result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%=request.getParameter("image")%>
<img src="<%=request.getParameter("image")%>">
</body>
</html>
在此处插入标题
">
在result.jsp页面中返回为null
帮助
首先,img
标记的src
只将图像路径作为字符串,所以不能在其中传递整个图像
现在您可以做的是:--当您在服务器上上载图像时,不要在请求中保存/传递整个图像文件,而只在您上载图像的请求中添加图像路径。在您的代码中,路径是path+*文件分隔符*+文件名
。将此消息传递给img标记的src,它将完成此任务。这里的文件分隔符可以是“/”
或“\\”
希望这有帮助
正如评论中所建议的,最好使用
您所采取的方法有两个错误。首先,在上面的servlet代码中,您的编码就像下载图像文件一样(您已将内容类型设置为图像)
其次,您没有以任何方式将映像文件与响应相关联。没有通过映像参数或流
现在,由于您已经混合了两种方法。您需要首先确定您需要什么。从您的解释中,我了解到您正在尝试在result.jpg中显示上载的图像。在这种情况下,您不需要发送内容类型为set的图像。在您的result.jsp中,img标记希望显示图像的路径。因此您需要将图像的路径设置为“image”属性作为响应
response.setAttribute("image", imagepath);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
图像路径应该是访问图像的有效URL。在result.jsp中,您需要:
request.getParameter("image");
要获取图像,请执行以下操作:
<img src='<%=request.getParameter("image")%>'>
”>
在servlet中设置一个请求属性,该属性将图像的完整路径返回给JSP
String fullpath = path + File.separator + filename
request.setAttribute("fullpath",fullpath);
您已经将请求转发到JSP
现在,使用
<%
String path = "";
if(request.getAttribute("fullpath")!=null)
path = request.getAttribute("fullpath").toString();
%>
然后使用该路径显示图像
<img src='<%=path%>'>
”>
我正在考虑将图像文件存储在服务器上的某个位置-如果您将文件存储在
/resources/image/someImage.jpg
之类的位置,则可以指定该路径(路径应位于应用程序内部,可通过浏览器使用上下文rrot访问该路径)image src属性,浏览器将指向该图像您可以编写一个新的servlet,然后使用goGet方法读取图像文件并将其写入响应输出流
您可以改进这段代码,我只是举个例子。Jeje,null的问题是因为它们是不同的东西;属性和参数,如果您这样做了:
request.setAttribute("image", file.getPath());
然后你需要做:
request.getAttribute("image")
完整路径是什么意思?如果映像存储在“d”驱动器上,则完整路径将是
request.setAttribute("image", file.getPath());
request.getAttribute("image")