SpringBoot-文件上传

本文介绍了一个简单的商品管理系统,包括商品信息的添加与展示过程。通过使用JSP页面和Spring MVC框架,实现了用户输入商品信息并上传图片的功能,并将商品信息保存至数据库。此外,还展示了如何从数据库中获取所有商品信息并在网页上进行展示。

addProduct.jsp页面代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>添加商品</h1>
<form action="addProductAction.do" method="post" enctype="multipart/form-data">
<p>商品名: <input type="text" name="prodname"/></p>
<p>描述: <input type="text" name="describe"/></p>
<p>单价: <input type="text" name="price"/></p>
<p>图片: <input type="file" name="file"/></p>
<p><input type="submit" value="提交" /></p>
</form>

</body>
</html>
ProductController控制器页面代码如下

package cn.bdqn.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.pojo.Product;
import cn.bdqn.service.ProductService;

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/addProduct")
    public String addProduct() {
        return "addProduct";
    }

    @RequestMapping("/show")
    public String show() {
        return "show";
    }

    @RequestMapping("/addProductAction")
    public String addProductAction(MultipartFile file, Product product,
            HttpServletRequest request) {
        try {
            // 原始的文件名
            String originalFilename = file.getOriginalFilename();

            String perfix = originalFilename.substring(0,
                    originalFilename.lastIndexOf("."));
            String suffix = originalFilename.substring(originalFilename
                    .lastIndexOf(".") + 1);

            String[] types = {"jpg","png","gif"};
            List<String> typeList = Arrays.asList(types);
            if(!typeList.contains(suffix)){
                throw new Exception("上传的文件类型不匹配");
            }
            
            String filename = perfix + "-" + System.currentTimeMillis() + "."
                    + suffix;
            product.setImgpath(filename);

//            String upload = request.getServletContext().getRealPath("upload")
//                    + File.separator;
            File newFile = new File("D:\\development\\fileserver\\" + filename);

            // 将文件写到本地

            file.transferTo(newFile);
            int addProduct = productService.addProduct(product);
            if (addProduct != 1) {
                throw new Exception("数据添加失败");
            }

            return "redirect:show.do";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:addProduct.do";
        }
    }

    @RequestMapping("/getAllProduct.do")
    @ResponseBody
    public List<Product> getAllProduct() {
        return productService.getAllProduct();
    }

}
成功后的显示页面show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="static/js/jquery.js"></script>
<script>
$(function(){
    $.post("getAllProduct.do",function(result){
        $.each(result,function(){
            var $li = $('<li>'+
                    '<img src="http://localhost:8080/fileserver/'+this.imgpath+'"/>'+
                    '<p>'+this.prodname+'</p>'+
                    '<p>'+this.describe+'</p>'+
                    '<p>'+this.price+'</p>'+
                    '</li>');        
            $(".product").append($li);
        });
    });
});
</script>
<title>Insert title here</title>
</head>
<body>
<ul class="product">
	
</ul>
</div>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值