【发布时间】:2011-07-27 10:19:41
【问题描述】:
我的代码正在上传图像并将图像保存到我的服务器,但我需要在我的 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.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>
小服务程序
上传图片
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);
}
}
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
帮助
【问题讨论】:
-
您能否告诉我们您是否愿意在预览之前将此图像存储在某个位置? ..会有所帮助,请回答
-
@Falcon 我需要将图像存储在我的 tomcar 服务器中。我在我的项目中创建了一个文件夹 images 并且图像被保存在该特定目录中,但我的问题是我无法在我的 jsp 页面中显示保存的图像
-
你做得对。现在只需在请求中传递带有上传图像名称的路径。
-
检查我的答案,在这种情况下第 1 点可能会有所帮助