我使用Spring MVC 3和Hibernate。我一直坚持显示图像。我完全能够上传图像,但成功上传后无法显示。请建议我在哪里出错。下面是我正在尝试的代码。
这是我的控制者:
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";
uploadfile 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>
uploadfileindex.jsp
<%@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>
答案 0 :(得分:0)
正如您所说,您的硬盘驱动器上有该图像,但您无法在网页上显示该图像。因此,您需要做的是将映像放在安装MVC的安装目录中。就像我们使用WAMP服务器一样,我们将图像存储在wamp\www\yourprojectfolder\image.jpg
中。像这样,您将直接通过网页获取图像。
答案 1 :(得分:0)
@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
。