SSM实现上传图片(上传到本地磁盘上)

图片上传保存的位置是在本地,一定要在tomcat进行设置的,如下图:
图片上传保存的位置是在本地,一定要在tomcat进行设置的,如下图:
图片上传保存的位置是在本地,一定要在tomcat进行设置的,如下图:
图片上传保存的位置是在本地,一定要在tomcat进行设置的,如下图:
图片上传保存的位置是在本地,一定要在tomcat进行设置的,如下图:

在这里插入图片描述

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760"></property>
</bean>

1 上传jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: SX
  Date: 2020/4/11
  Time: 11:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>



<form action="/upload" method="post" enctype="multipart/form-data">
    <h3>上传图片</h3>
    <input type="file" name="file"><br>
    <button type="submit">上传</button>
</form>
</body>
</html>

Controller层

package com.hp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author SX
 * @date 2020/5/25 -9:01
 */
@Controller
public class BookController {

@RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String bookselect(Model model, HttpServletRequest req,@RequestParam("file") MultipartFile file){

 String realpath="D:\\SpringMvc\\day03-Springmvc-fileupload\\src\\main\\webapp\\img";
    //获取文件名字
   String filename=file.getOriginalFilename();
   //存放路径
   filename= UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
   String path=realpath+"\\"+filename;
   try {
        file.transferTo(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("上传失败");
    }
    model.addAttribute("list",filename);
    return "success";
}

}

succes.jsp 上传图片回显

<%--
  Created by IntelliJ IDEA.
  User: SX
  Date: 2020/4/11
  Time: 11:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${list}
上传成功

<img src="${pageContext.request.contextPath}/img/${list}">
</body>
</html>

上传成功图片并且回显
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值