我的代码是上传图像并将图像保存到我的服务器,但我需要在我的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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
<%= request.getParameter("image")%>在result.jsp页面中返回null
救命
我在考虑你在服务器上的某个地方存储一个图像文件 -
<context root>/resources/image/someImage.jpg那么您可以提供该路径(路径应该在应用程序内部,可以通过浏览器访问context rrot)image src属性和浏览器将指向该图像.例如
<image src="context root/displayServlet?image=someImage.jpg">
Run Code Online (Sandbox Code Playgroud)
servlet代码可能看起来像 -
编辑: - 有关更好的代码,请参阅如何从JSP页面中的数据库中检索和显示图像?
try{
String fileName = request.getParameter("image");
FileInputStream fis = new FileInputStream(new File("d:\\"+fileName));
BufferedInputStream bis = new BufferedInputStream(fis);
response.setContentType(contentType);
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
for (int data; (data = bis.read()) > -1;) {
output.write(data);
}
}
catch(IOException e){
}finally{
// close the streams
}
Run Code Online (Sandbox Code Playgroud)
你可以改进这段代码,我只是举个例子.
| 归档时间: |
|
| 查看次数: |
66210 次 |
| 最近记录: |