这几天在弄毕设,有几个需求是要上传图片的,看了很多教程,各有各的说法,关键点、细节很多说不明白,自己倒腾了几天,弄出来了,给大家分享一下。
上传图片的JSP
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>图片上传</title>
</head>
<body>
<form action="client_addClient" method="post" enctype="multipart/form-data">
用户名 :<input type="text" name="client.clientName" id="clientName"><br>
密码 :<input type="password" name="client.clientPass" id="clientPass"><br>
照片 :<input type="file" name="clientImg"/><br>
<h1> </h1>
<input type="submit" value="addClient">
</form>
</body>
</html>
Action
private File clientImg;
private String clientImgFileName;
public File getClientImg() {
return clientImg;
}
public void setClientImg(File clientImg) {
this.clientImg = clientImg;
}
public String getClientImgFileName() {
return clientImgFileName;
}
public void setClientImgFileName(String clientImgFileName) {
this.clientImgFileName = clientImgFileName;
}
public String addClient() throws FileNotFoundException, IOException{
client.setClientImg(clientImgFileName);
BufferedImage sourceImg =ImageIO.read(new FileInputStream(clientImg));
//获取图片长宽和大小,长宽限制为600*800, 大小2M以内 1M是 1048576 个字节
//能获取长宽,就是图片,不能就不是图片,上传失败
if(sourceImg.getWidth()<600 && sourceImg.getHeight()<800 && clientImg.length()/1048576<2){
if(clientImg!=null){
//获取绝对路径
String filePath = "C:/Users/Workspaces/MyEclipse 10/AjaxTest/WebRoot/upload/client/";
//重命名 UUID+JPG
String fileName = UUIDUtils.getUUID()+".jpg";
File diskFile = new File(filePath+fileName);
// 文件上传,使用FileUtils工具类
try {
FileUtils.copyFile(clientImg, diskFile);
client.setClientImg("upload/client/"+fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientService.addClient(client);
}
}else{
return "addFail";
}
return "addClient_succ";
}
将图片上传、保存到本地路径,将路径存到数据库。此时我图片的路径是这样的 upload/client/e814430003d741ed8f0300db7bc13866.jpg 。 upload/client/ 是我的相对路径,这样放方便等会拿
并且我的图片是在本地查看,其实是在项目工程里的。upload/client 是WebRoot下的文件夹
图片显示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri='/struts-tags' prefix='s' %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示个人信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<table>
<tr>
<th>
ID
</th>
<th>
name
</th>
<th>
password
</th>
<th>
photo
</th>
</tr>
<tr>
<td>
${clients.clientId}
</td>
<td>
${clients.clientName}
</td>
<td>
${clients.clientPass}
</td>
<td>
<span style="white-space:pre"> </span>
<img width="100" height="100" src="${clients.clientImg}" />
</td>
</tr>
</table>
</div>
</body>
</html>
像正常取实体类的属性一样就可以,因为我的图片的路径已经加入了相对于jsp的路径,所以可以显示。
以上就是SSH的图片上传有显示了,大家参考。
这样设置本地上传有个问题,就是新加图片上传后,必须刷新上传的文件夹以及重启服务器才能显示,怎么解决这个问题暂时没有好办法。