主要内容:以实现文件上传目的,进行项目搭建。
IDEA 2018.3;SpringMVC 4.3.21.RELEASE
使用springMVC框架要实现文件的上传下载要额外添加commons
属性 | 描述 |
---|---|
defaultEncoding | 表示用来解析request请求的默认编码格式,当没有指定的时候根据Servlet规范会使用默认值ISO-8859-1。当request自己指明了它的编码格式的时候就会忽略这里指定的defaultEncoding。 |
uploadTempDir | 设置上传文件时的临时目录,默认是Servlet容器的临时目录。 |
maxUploadSize | 设置允许上传的总的最大文件大小,以字节为单位计算。当设为-1时表示无限制,默认是-1。 |
maxUploadSizePerFile | 跟maxUploadSize差不多,不过maxUploadSizePerFile是限制每个上传文件的大小,而maxUploadSize是限制总的上传文件大小。 |
maxInMemorySize | 设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240。 |
resolveLazily | 为true时,启用推迟文件解析,以便在UploadAction中捕获文件大小异常。 |
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="10485760"/>
<property name="maxUploadSizePerFile" value="10485760"/>
bean>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传title>
head>
<body>
<h1>文件上传h1>
<form action="test/upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>请选择文件:td>
<td><input type="file" name="file">td>
tr>
<tr>
<td><input type="submit" value="上传">td>
tr>
table>
form>
body>
html>
@Controller
@RequestMapping("/test")
public class Servlet {
@PostMapping("/upload")
public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
//如果文件不为空,写入上传路径
if (!file.isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/img");
//上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path, filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
System.out.println(path);
//将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
return "success";
} else {
return "error";
}
}
}
注:
这样在IDEA中的保存路径为:…\target\springMVC\img文件下下面。如果想更改路径,自己更改path变量即可。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>successtitle>
head>
<body>
<h2>文件上传成功!h2>
body>
html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>errortitle>
head>
<body>
<h2>文件上传失败!h2>
body>
html>
//实现序列化接口
public class Img implements Serializable {
private String desc;
private MultipartFile file;
public Img() {
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
registerForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传title>
head>
<body>
<body>
<h1>文件上传h1>
<form action="test/register" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>描述:td>
<td><input type="text" name="desc">td>
tr>
<tr>
<td>请选择文件:td>
<td><input type="file" name="file">td>
tr>
<tr>
<td>上传:td>
<td><input type="submit" value="上传">td>
tr>
table>
form>
body>
body>
html>
img.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传title>
head>
<body>
<h3>文件上传h3>
${requestScope.img.file.originalFilename }
a>
body>
html>
@RequestMapping(value = "/register")
public String register(HttpServletRequest request,
@ModelAttribute Img img,
Model model) throws Exception {
System.out.println(img.getDesc());
//如果文件不为空,写入上传路径
if (!img.getFile().isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/img");
//上传文件名
String filename = img.getFile().getOriginalFilename();
File filepath = new File(path, filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//将上传文件保存到一个目标文件当中
img.getFile().transferTo(new File(path + File.separator + filename));
//将用户添加到model
model.addAttribute("img", img);
return "img";
} else {
return "error";
}
}
springMVC.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="cn.spring.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="10485760"/>
<property name="maxUploadSizePerFile" value="10485760"/>
bean>
beans>
Servlet.java
package cn.spring.controller;
import cn.spring.bean.Img;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@Controller
@RequestMapping("/test")
public class Servlet {
@PostMapping("/upload")
public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
//如果文件不为空,写入上传路径
if (!file.isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/img");
//上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path, filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
System.out.println(path);
//将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
return "success";
} else {
return "error";
}
}
@RequestMapping(value = "/register")
public String register(HttpServletRequest request,
@ModelAttribute Img img,
Model model) throws Exception {
System.out.println(img.getDesc());
//如果文件不为空,写入上传路径
if (!img.getFile().isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/img");
//上传文件名
String filename = img.getFile().getOriginalFilename();
File filepath = new File(path, filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//将上传文件保存到一个目标文件当中
img.getFile().transferTo(new File(path + File.separator + filename));
//将用户添加到model
model.addAttribute("img", img);
return "img";
} else {
return "error";
}
}
}