gpt4 book ai didi

java - 在 servlet 的 jsp 页面中显示图像

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:03 24 4
gpt4 key购买 nike

<分区>

我的代码正在上传图像并将图像保存到我的服务器,但我需要在我的 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>&nbsp;</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">&nbsp;

</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);

}

}

结果.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>

<%=request.getParameter("图像")%> 在 result.jsp 页面中返回为 null

帮助

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com