求SpringMVC大文件上传详解及实例代码
SpringMVC的文件上传非常简便,首先导入文件上传依赖的魔兽改名器+源码jar:
<!-- 文件上传所依赖的jar包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
在springMVC-servlet.xml配置文件中配置文件解析器:
<!--1**即1M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 -->
<!--文件上传解析器-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value=""/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
</bean>
注意解析器的shadowsock ios 源码id必须等于multipartResolver,否则上传会出现异常
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.File;
@Controller
public class FileController {
/
*** 上传单个文件操作
* MultipartFile file就是上传的文件
* @return
*/
@RequestMapping(value = "/upload1.html")
public String fileUpload1(@RequestParam("file") MultipartFile file) {
try {
//将上传的文件存在E:/upload/下
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/upload/",
file.getOriginalFilename()));
} catch (Exception e) {
e.printStackTrace();
}
//上传成功返回原来页面
return "/file.jsp";
}}
上传文件时,Controller的bkengine源码分析方法中参数类型是MultipartFile即可将文件映射到参数上。
错题集源码页面:
错题集源码file.jsp:
错题集源码<form method="post" action="/upload1.html" enctype="multipart/form-data">
错题集源码<input type="file" name="file"/>
错题集源码<button type="submit" >提交</button>
错题集源码</form>
错题集源码2024-11-30 20:32
2024-11-30 20:21
2024-11-30 20:15
2024-11-30 19:20
2024-11-30 19:00