图片上传保存的位置是在本地,一定要在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>
上传成功图片并且回显