Java 我可以将图像存储在我的电脑驱动器中。但无法在页面上显示
我正在使用SpringMVC3和Hibernate。我一直在展示图像。我完全可以上传图像,但成功上传后无法显示。请告诉我哪里走错了。下面是我正在尝试的代码 这是我的控制器:Java 我可以将图像存储在我的电脑驱动器中。但无法在页面上显示,java,html,jsp,spring-mvc,Java,Html,Jsp,Spring Mvc,我正在使用SpringMVC3和Hibernate。我一直在展示图像。我完全可以上传图像,但成功上传后无法显示。请告诉我哪里走错了。下面是我正在尝试的代码 这是我的控制器: private String uploadFolderPath; ServletConfig config; public String getUploadFolderPath() { return uploadFolderPath; } public void setUploadFolderPath
private String uploadFolderPath;
ServletConfig config;
public String getUploadFolderPath() {
return uploadFolderPath;
}
public void setUploadFolderPath(String uploadFolderPath) {
this.uploadFolderPath = uploadFolderPath;
}
@RequestMapping(value = "/uploadfile",method = RequestMethod.GET)
public String getUploadForm(Model model) {
model.addAttribute(new UploadItem());
return "/uploadfile";
}
@RequestMapping(value = "/uploadfile",method = RequestMethod.POST)
public String create(UploadItem uploadItem, HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors,
HttpSession session) {
try {
MultipartFile filea = uploadItem.getFileData();
InputStream inputStream = null;
OutputStream outputStream = null;
if (filea.getSize() > 0) {
inputStream = filea.getInputStream();
// File realUpload = new File("C:/");
outputStream = new FileOutputStream("C:\\images\\"
+ filea.getOriginalFilename());
System.out.println("====22=========");
System.out.println(filea.getOriginalFilename());
System.out.println("=============");
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
System.out.println("===ddd=======");
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
session.setAttribute("uploadFile", "C:\\images\\"
+ filea.getOriginalFilename());
}
} catch (Exception e) {
e.printStackTrace();
}
return "uploadfileindex";
}
@RequestMapping(value = "/uploadfileindex",method = RequestMethod.GET)
public String showRegistration(Model model) {
return "uploadfileindex";
}
// Process the form.
@RequestMapping(value = "/uploadfileindex",method = RequestMethod.POST)
public String processRegistration(BindingResult result) {
return "uploadfileindex";
上传文件jsp
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%@ page session="false"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Upload Example</title>
<script language="JavaScript">
function Validate()
{
var image =document.getElementById("image").value;
if(image!=''){
var checkimg = image.toLowerCase();
if (!checkimg.match(/(\.jpg|\.png|\.JPG|\.PNG|\.jpeg|\.JPEG)$/)){
alert("Please enter Image File Extensions .jpg,.png,.jpeg");
document.getElementById("image").focus();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form:form modelAttribute="uploadItem" name="frm" method="post"
enctype="multipart/form-data" onSubmit="return Validate();">
<fieldset><legend>Upload File</legend>
<table>
<tr>
<td><form:label for="fileData" path="fileData">File</form:label><br />
</td>
<td><form:input path="fileData" id="image" type="file" /></td>
</tr>
<tr>
<td><br />
</td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</fieldset>
</form:form>
</body>
</html>
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%@ page session="true"%>
<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Welcome</title>
</head>
<body>
<h2><a href="uploadfile.jsp">Upload Example</a></h2>
<%
if (session.getAttribute("uploadFile") != null
&& !(session.getAttribute("uploadFile")).equals("")) {
%>
<h3>Uploaded File</h3>
<br>
<img src="<%=session.getAttribute("uploadFile")%>" alt="Upload Image" />
<%
session.removeAttribute("uploadFile");
}
%>
</body>
</html>
上传示例
函数验证()
{
var image=document.getElementById(“image”).value;
如果(图像!=''){
var checkimg=image.toLowerCase();
如果(!checkimg.match(/(\.jpg\.png\.jpg\.png\.png\.jpeg\.jpeg)$/){
警报(“请输入图像文件扩展名.jpg、.png、.jpeg”);
document.getElementById(“image”).focus();
返回false;
}
}
返回true;
}
上载文件
文件
uploadfileindex.jsp
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%@ page session="false"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Upload Example</title>
<script language="JavaScript">
function Validate()
{
var image =document.getElementById("image").value;
if(image!=''){
var checkimg = image.toLowerCase();
if (!checkimg.match(/(\.jpg|\.png|\.JPG|\.PNG|\.jpeg|\.JPEG)$/)){
alert("Please enter Image File Extensions .jpg,.png,.jpeg");
document.getElementById("image").focus();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form:form modelAttribute="uploadItem" name="frm" method="post"
enctype="multipart/form-data" onSubmit="return Validate();">
<fieldset><legend>Upload File</legend>
<table>
<tr>
<td><form:label for="fileData" path="fileData">File</form:label><br />
</td>
<td><form:input path="fileData" id="image" type="file" /></td>
</tr>
<tr>
<td><br />
</td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</fieldset>
</form:form>
</body>
</html>
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%@ page session="true"%>
<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Welcome</title>
</head>
<body>
<h2><a href="uploadfile.jsp">Upload Example</a></h2>
<%
if (session.getAttribute("uploadFile") != null
&& !(session.getAttribute("uploadFile")).equals("")) {
%>
<h3>Uploaded File</h3>
<br>
<img src="<%=session.getAttribute("uploadFile")%>" alt="Upload Image" />
<%
session.removeAttribute("uploadFile");
}
%>
</body>
</html>
欢迎
上传文件
“alt=”上传图像“/>
正如您所说,您的硬盘上有该图像,但无法在网页上显示。因此,您需要做的是,将图像放在安装MVC的安装目录中。就像我们使用WAMP服务器一样,我们将图像存储在WAMP\www\yourprojectfolder\image.jpg
中您将直接通过网页获取图像
@Controller
@RequestMapping(value="/imageServer")
public class ImageServer {
@RequestMapping(value="/{imageUrl:.+}")
public void getImageByUrl(@PathVariable String imageUrl,
HttpServletResponse response) throws IOException
{
String filesFolder = "C:/images";
File finalImage = new File(filesFolder+"/"+imageUrl);
FileInputStream fileStream=new FileInputStream(finalImage);
try {
response.getOutputStream().write(IOUtils.toByteArray(fileStream));
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
在JSP页面上,选择:
<img src"${pageContext.request.contextPath}/imageServer/image.jpg">
因此,您可以在您的webo页面中看到
C:/images/image.jpg
。在控制台中,我得到了这个。当我复制此源代码并输入url时,会显示图像。但我想在成功完成ploadhey后进入页面。如果您弄错了,请将您的图像放在最终安装文件夹中。谢谢…我会尝试..如果有任何问题,我会让您知道..这是一个公司您提供的控制器我应该添加此控制器还是替换为我的控制器?是的,添加此控制器,并从jsp调用它,使用文件系统的图像名称,如示例所示。可以告诉我您在浏览器中遇到的错误吗?在浏览器中,当我将鼠标悬停在其上时,会弹出一条消息,说明加载给定的urlchange ImageServer t失败o JSP中的imageServer