package org.swinglife.controller;
import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.ModelAndView;
/***
* 文件上传控制类
* @author swinglife
*
*/
@Controller
public class FileUploadController {
//通过Spring的autowired注解获取spring默认配置的request
@Autowired
private HttpServletRequest request;
@RequestMapping("filesUpload")
public String filesUpload(@RequestParam("files") MultipartFile[] files) {
//判断file数组不能为空并且长度大于0
if(files!=null&&files.length>0){
//循环获取file数组中得文件
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];
//保存文件
saveFile(file);
}
}
// 重定向
return "redirect:/list.html";
}
/***
* 保存文件
* @param file
* @return
*/
private boolean saveFile(MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
+ file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/***
* 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile
*
* @param file
* @return
*/
@RequestMapping("fileUpload")
public String fileUpload(@RequestParam("file") MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
+ file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
// 重定向
return "redirect:/list.html";
}
/***
* 读取上传文件中得所有文件并返回
*
* @return
*/
@RequestMapping("list")
public ModelAndView list() {
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
ModelAndView mav = new ModelAndView("list");
File uploadDest = new File(filePath);
String[] fileNames = uploadDest.list();
for (int i = 0; i < fileNames.length; i++) {
System.out.println(fileNames[i]);
}
return mav;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpringMVC_01.zip (66个子文件)
SpringMVC_01
.project 1009B
.DS_Store 6KB
WebContent
WEB-INF
lib
spring-aspects-3.2.0.RC1.jar 68KB
spring-instrument-tomcat-3.2.0.RC1.jar 10KB
aspectjtools.jar 8.5MB
spring-jms-3.2.0.RC1.jar 196KB
spring-beans-3.2.0.RC1.jar 587KB
spring-context-3.2.0.RC1.jar 844KB
spring-oxm-3.2.0.RC1.jar 71KB
spring-tx-3.2.0.RC1.jar 235KB
spring-jdbc-3.2.0.RC1.jar 389KB
commons-pool-1.3.jar 61KB
spring-web-3.2.0.RC1.jar 605KB
spring-webmvc-3.2.0.RC1.jar 596KB
spring-expression-3.2.0.RC1.jar 189KB
spring-test-3.2.0.RC1.jar 406KB
spring-core-3.2.0.RC1.jar 843KB
spring-aop-3.2.0.RC1.jar 325KB
spring-orm-3.2.0.RC1.jar 377KB
commons-dbcp.jar 118KB
spring-instrument-3.2.0.RC1.jar 7KB
org.aspectj.matcher.jar 797KB
aspectjweaver.jar 1.6MB
spring-webmvc-portlet-3.2.0.RC1.jar 184KB
spring-struts-3.2.0.RC1.jar 35KB
log4j-1.2.15.jar 383KB
commons-io-1.3.2.jar 86KB
aspectj-DEVELOPMENT-20110210194753.jar 12.15MB
commons-fileupload-1.2.1.jar 56KB
aopalliance-1.0.jar 4KB
aspectjrt.jar 114KB
json_simple-1.1.jar 16KB
commons-io-1.4.jar 106KB
commons-logging-1.0.4.jar 37KB
mysql-connector-java-5.1.7-bin.jar 693KB
commons-lang-2.3.jar 240KB
web.xml 1KB
upload
.DS_Store 6KB
.DS_Store 6KB
page
fileUpload.jsp 537B
other.jsp 357B
succ.jsp 398B
filesUpload.jsp 673B
uploadSucc.jsp 364B
list.jsp 364B
home.jsp 684B
META-INF
MANIFEST.MF 39B
src
root-context.xml 673B
mvc-context.xml 2KB
org
swinglife
controller
HomeController.java 819B
OtherController.java 393B
UserController.java 2KB
FileUploadController.java 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 335B
org.eclipse.jdt.core.prefs 357B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 481B
.jsdtscope 491B
build
classes
root-context.xml 673B
mvc-context.xml 2KB
org
swinglife
controller
UserController.class 1KB
OtherController.class 586B
HomeController.class 1KB
FileUploadController.class 3KB
.classpath 648B
共 66 条
- 1
SwingPyzf
- 粉丝: 405
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页