【实用】JSP页面图片回显的几种方式

本文介绍了两种实现JSP页面图片回显的方法:一种是在Controller层进行配置,另一种是在JSP页面直接处理。配合CommonUtil.java公共类和common.properties图片地址属性文件,可以灵活选择适合的回显方式。
摘要由CSDN通过智能技术生成

方式一   在Controller层中进行配置

@RequestMapping(value = "/StudentSideController_goWorksDetail.do", method = RequestMethod.GET)
	public String goWorksDetail(HttpSession session, ModelMap data, Integer worksId) throws Exception {
		String worksPath = CommonUtil.getConfig("common","STUDENT_WORK_FILE_PATH");
		StringBuffer url = request.getRequestURL();
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
		String realpath = tempContextUrl + "upload/studentwork";

		PageData param = getPageData();
		param.put("orgId", session.getAttribute("orgId"));
		param.put("studentId", session.getAttribute("studentId"));
		param.put("worksId", worksId);
		try {
			PageData works = studentWorkFacade.getWorksManage(param);
			if (null != works && null != w
以下是一个简单的 Java Web 图片上传回显JSP 和 Servlet 完整代码示例。 JSP 页面代码(upload.jsp): ```jsp <!DOCTYPE html> <html> <head> <title>图片上传回显</title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit" value="上传"> </form> <% if (request.getAttribute("imageUrl") != null) { %> <img src="<%= request.getAttribute("imageUrl") %>"> <% } %> </body> </html> ``` Servlet 代码(UploadServlet.java): ```java import java.io.File; import java.io.IOException; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload") public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String UPLOAD_DIR = "uploads"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String applicationPath = request.getServletContext().getRealPath(""); String uploadFilePath = applicationPath + File.separator + UPLOAD_DIR; File fileSaveDir = new File(uploadFilePath); if (!fileSaveDir.exists()) { fileSaveDir.mkdirs(); } Part part = request.getPart("image"); String fileName = UUID.randomUUID().toString() + "_" + part.getSubmittedFileName(); String filePath = uploadFilePath + File.separator + fileName; part.write(filePath); String imageUrl = request.getContextPath() + "/" + UPLOAD_DIR + "/" + fileName; request.setAttribute("imageUrl", imageUrl); request.getRequestDispatcher("upload.jsp").forward(request, response); } } ``` 注意事项: - 在 JSP 中,当检查请求属性 "imageUrl" 是否存在时,请使用 `request.getAttribute("imageUrl") != null`。 - 在 Servlet 中,将上传的文件保存到服务器上的路径可以使用 `request.getServletContext().getRealPath("")` 获取当前应用程序的根目录。 - 在 Servlet 中,获取上传的文件的文件名可以使用 `part.getSubmittedFileName()` 方法。 - 在 Servlet 中,通过 `request.getContextPath()` 获取应用程序的上下文路径,例如 "/myapp"。 - 在 Servlet 中,使用 `request.getRequestDispatcher("upload.jsp").forward(request, response)` 转发回 JSP 页面,并在 JSP 页面中显示上传的图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值