JSP_Spring MVC中上传文件实例,SpringMVC(注解)上传文件需要注
</body>
import org.springframework.web.bind.annotation.RequestParam;
<props>
<form action="<%=request.getContextPath()%>/upload/filesUpload" method="POST" enctype="multipart/form-data">
long getSize() //获取文件的字节大小,单位byte
.getRealPath("https://www.xp.cn/") + "upload/" + file.getOriginalFilename();
@RequestMapping("/toUpload")
import org.springframework.web.multipart.MultipartFile;
for (int i = 0; i < files.length; i++) {
import org.springframework.stereotype.Controller;
package com.ljq.web.controller.annotation;
<h1>文件过大,请重新选择</h1>
到此文件上传开发就结束了。
*/File saveDir = new File(filePath);
public class UploadController {
if (!file.isEmpty()) {
} catch (Exception e) {
import javax.servlet.http.HttpServletRequest;
* 保存文件
</head>
<property value="UTF-8"/>
*
String getContentType() //获取文件MIME类型
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
/**
<property value="200000"/>
import org.springframework.web.bind.annotation.RequestMapping;
/***
@Controller
<property value="40960" />
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
e.printStackTrace();
</form>
MultipartFile file = files[i];
// 保存文件
}
if (files != null && files.length > 0) {
// 重定向
复制代码 代码如下:
* @returnreturn "redirect:/upload/toUpload";
<title>上传图片</title>
return false;
<%@ page language="java" contentType="text/html; charset=UTF-8"
/**
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
return "/upload";
* @param files
* @return
// 保存的文件路径(如果用的是Tomcat服务器,文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\upload\\文件夹中 )
file.transferTo(saveDir);
<input type="submit" value="上传图片"/>
1、form的enctype="multipart/form-data",这个是上传文件必须的
@RequestMapping("/upload")
</bean>
复制代码 代码如下:
</property>}
*
* 上传图片
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
return true;
<property>
SpringMVC(注解)上传文件需要注意的几个地方:
// 转存文件
String filePath = request.getSession().getServletContext()
上传文件的核心UploadController类
上传文件内容过大时的提示页面/WEB-INF/jsp/error_fileupload.jsp
try {<head>
* 上传图片
* @param file
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<bean>
用于上传的表单页面/WEB-INF/jsp/upload.jsp
import java.io.File;
*
String getName() //获取表单中文件组件的名字
<body>
yourfile: <input type="file"/><br/>
*/
}
public String toUpload() {
* @author Administrator
复制代码 代码如下:
<script type="text/javascript" src=""></script>
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
* @param request
}
boolean isEmpty() //是否为空
<!-- 最大内存大小 (10240)-->
saveFile(request, file);
<%@ page language="java" pageEncoding="UTF-8"%>
复制代码 代码如下:
if (!saveDir.getParentFile().exists())<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
private boolean saveFile(HttpServletRequest request, MultipartFile file) {
MultipartFile类常用的一些方法:
*
void transferTo(File dest) //保存到一个目标文件中
</bean>
InputStream getInputStream() //返回文件流
HttpServletRequest request) {
<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
public String filesUpload(@RequestParam("myfiles") MultipartFile[] files,
2、applicationContext.xml配置: yourfile: <input type="file"/><br/>
}
</props>
*/
}
<bean>
pageEncoding="UTF-8"%>
<html>
}
</html>
<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/jsp/6560.shtml
