ssm框架实现图⽚上传功能(附完整代码)如果本⽂对您有所帮助,可以点⼀下赞
1、本⽂实现图⽚上传并回显
2、实现思路如下
1. 图⽚上传到服务器
2. 给图⽚重命名,重命名是为了图⽚有唯⼀的名字
3. 将名字写进数据库
4. 通过数据库获取图⽚名,并以此为根据显⽰到页⾯
3、实现代码如下
<!-- ⽂件上传的jar包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
">
<!-- ⽂件上传的解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<!-- 上传图⽚的最⼤尺⼨ 10M-->
<property name="maxUploadSize" value="10485760"></property>
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
upload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx", ContextPath());%>
<html>
<head>
<title>图⽚上传demo</title>
</head>
<body>
<form action="${ctx}/product/saveImaga" method="post" enctype="multipart/form-data">
图⽚:<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
listImages.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<% pageContext.setAttribute("ctx", ContextPath()); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="cdn.bootcss/bootstrap/3.3.7/css/bootstrap.min.css"/> <script src="cdn.bootcss/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>list</title>
<style type="text/css">
#images{
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<table class="table table-bordered table-hover">
<tr>
<th>序号</th>
<th>图⽚</th>
</tr>
<c:forEach items="${lists}" var="product">
<tr>
<td>${product.pid}</td>
getsavefilename<td>
<c:if test="${product.pimage != null}">
<img alt="" src="${ctx}/upload/${product.pimage}" id="images">
</c:if>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
ProductController.java
import lingnan.pojo.Product;
import lingnan.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping("/listImages")
public String list(Model model) {
List<Product> lists = productService.list();
model.addAttribute("lists", lists);
System.out.println(":::来到请求listImages");
return "forward:/listImages.jsp";
}
@RequestMapping("/saveImaga")
public String saveImaga( MultipartFile file, HttpServletRequest request) throws IOException {
//String name = UUID.randomUUID().toString().replaceAll("-", "");
//设置图⽚上传路径,是⽬标⽂件夹的路径
String filePath = Session().getServletContext().getRealPath("/upload");
System.out.println("filePath:::"+filePath);
// 获取原始图⽚的扩展名
String originalFilename = OriginalFilename();
System.out.println("originalFilename:::"+originalFilename);
// ⽣成⽂件新的名字
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + originalFilename; System.out.println("newFileName:::"+newFileName);
// 封装上传⽂件位置的全路径
File targetFile = new File(filePath, newFileName);
// 保存到数据库
Product product=new Product();
product.setPimage(newFileName);
productService.save(product);
//重定向到查看图⽚的⽅法上去
return "redirect:/product/listImages";
}
}
ProductServiceImpl.java
import lingnan.mapper.ProductMapper;
import lingnan.pojo.Product;
import lingnan.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public List<Product> list() {
return productMapper.list();
}
@Override
public int save(Product product) {
return productMapper.save(product);
}
@Override
public String save(MultipartFile file, Product product, ModelMap map) throws IOException { //保存图⽚的路径,图⽚上传成功后,将路径保存到数据库
String filePath = "E:\\upload";
//String filePath = Session().getServletContext().getRealPath("/userhead"); // 获取原始图⽚的扩展名
String originalFilename = OriginalFilename();
// ⽣成⽂件新的名字
String newFileName = UUID.randomUUID() + originalFilename;
// 封装上传⽂件位置的全路径
File targetFile = new File(filePath, newFileName);
// 保存到数据库
product.setPimage(newFileName);
productMapper.save(product);
return "redirect:/listImages";
}
}
ProductService.java
package lingnan.service;
import lingnan.pojo.Product;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
public interface ProductService {
List<Product> list();
int save(Product product);
String save(MultipartFile file, Product product, ModelMap map) throws IOException; }
Product.java
package lingnan.pojo;
import java.io.Serializable;
public class Product implements Serializable {
private Integer pid;
private String pimage;
public Product() {
}
public Product(Integer pid, String pimage) {
this.pid = pid;
this.pimage = pimage;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
}
ProductMapper.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论