package cn.edu.jseti.controller;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import cn.edu.jseti.domain.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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;
/**
* @author Cody
* @version V1.0
*/
@Controller
public class FileUploadController {
@RequestMapping(value="/{formName}")
public String loginForm(@PathVariable String formName) {
// 动态跳转页面
return formName;
}
//上传文件会自动绑定到MultipartFile中
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest request,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) throws Exception {
System.out.println(description);
//如果文件不为空,写入上传路径
if(!file.isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/images/");
//上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path,filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
return "success";
} else {
return "error";
}
}
@RequestMapping(value="/register")
public String register(HttpServletRequest request,
@ModelAttribute User user,
Model model) throws Exception {
System.out.println(user.getUsername());
//如果文件不为空,写入上传路径
if(!user.getImage().isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/images/");
//上传文件名
String filename = user.getImage().getOriginalFilename();
File filepath = new File(path,filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//将上传文件保存到一个目标文件当中
user.getImage().transferTo(new File(path + File.separator + filename));
//将用户添加到model
model.addAttribute("user", user);
return "userInfo";
} else {
return "error";
}
}
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,
@RequestParam("filename") String filename,
Model model)throws Exception {
//下载文件路径
String path = request.getServletContext().getRealPath("/images/");
File file = new File(path + File.separator + filename);
HttpHeaders headers = new HttpHeaders();
//下载显示的文件名,解决中文名称乱码问题
String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
//通知浏览器以attachment(下载方式)打开图片
headers.setContentDispositionFormData("attachment", downloadFielName);
//application/octet-stream : 二进制流数据(最常见的文件下载)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringMVC框架实现文件的上传和下载
共73个文件
jar:46个
jsp:10个
xml:5个
4星 · 超过85%的资源 需积分: 44 685 下载量 184 浏览量
2017-04-05
13:50:56
上传
评论 19
收藏 14.57MB RAR 举报
温馨提示
SpringMVC框架实现文件的上传和下载项目案例源码
资源推荐
资源详情
资源评论
收起资源包目录
FileUpload.rar (73个子文件)
FileUpload
.project 1KB
.settings
.jsdtscope 567B
org.eclipse.wst.common.component 482B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
cn
edu
jseti
domain
User.java 616B
controller
FileUploadController.java 4KB
build
classes
cn
edu
jseti
domain
User.class 917B
controller
FileUploadController.class 5KB
WebContent
WEB-INF
WEB-INF
content
success.jsp 371B
userInfo.jsp 493B
error.jsp 371B
uploadForm.jsp 736B
registerForm.jsp 733B
springmvc-config.xml 2KB
web.xml 1KB
lib
spring-context-support-4.2.0.RELEASE.jar 176KB
spring-instrument-tomcat-4.2.0.RELEASE.jar 10KB
spring-aop-4.2.0.RELEASE.jar 355KB
spring-messaging-4.2.0.RELEASE.jar 362KB
spring-orm-4.2.0.RELEASE.jar 455KB
spring-context-4.2.0.RELEASE.jar 1.03MB
spring-instrument-4.2.0.RELEASE.jar 7KB
spring-jms-4.2.0.RELEASE.jar 273KB
spring-webmvc-portlet-4.2.0.RELEASE.jar 173KB
spring-tx-4.2.0.RELEASE.jar 257KB
spring-core-4.2.0.RELEASE.jar 1MB
commons-io-2.4.jar 181KB
spring-websocket-4.2.0.RELEASE.jar 418KB
spring-jdbc-4.2.0.RELEASE.jar 412KB
spring-aspects-4.2.0.RELEASE.jar 57KB
spring-test-4.2.0.RELEASE.jar 537KB
commons-fileupload-1.3.1.jar 67KB
spring-oxm-4.2.0.RELEASE.jar 81KB
commons-logging-1.1.1.jar 59KB
spring-web-4.2.0.RELEASE.jar 748KB
spring-beans-4.2.0.RELEASE.jar 712KB
spring-webmvc-4.2.0.RELEASE.jar 845KB
spring-expression-4.2.0.RELEASE.jar 256KB
content
success.jsp 387B
userInfo.jsp 496B
error.jsp 367B
uploadForm.jsp 736B
registerForm.jsp 733B
springmvc-config.xml 2KB
web.xml 1KB
lib
spring-context-support-4.2.0.RELEASE.jar 176KB
spring-instrument-tomcat-4.2.0.RELEASE.jar 10KB
spring-aop-4.2.0.RELEASE.jar 355KB
spring-messaging-4.2.0.RELEASE.jar 362KB
spring-orm-4.2.0.RELEASE.jar 455KB
spring-context-4.2.0.RELEASE.jar 1.03MB
spring-instrument-4.2.0.RELEASE.jar 7KB
spring-jms-4.2.0.RELEASE.jar 273KB
spring-webmvc-portlet-4.2.0.RELEASE.jar 173KB
spring-tx-4.2.0.RELEASE.jar 257KB
spring-core-4.2.0.RELEASE.jar 1MB
commons-io-2.4.jar 181KB
spring-websocket-4.2.0.RELEASE.jar 418KB
spring-jdbc-4.2.0.RELEASE.jar 412KB
spring-aspects-4.2.0.RELEASE.jar 57KB
spring-test-4.2.0.RELEASE.jar 537KB
commons-fileupload-1.3.1.jar 67KB
spring-oxm-4.2.0.RELEASE.jar 81KB
commons-logging-1.1.1.jar 59KB
spring-web-4.2.0.RELEASE.jar 748KB
spring-beans-4.2.0.RELEASE.jar 712KB
spring-webmvc-4.2.0.RELEASE.jar 845KB
spring-expression-4.2.0.RELEASE.jar 256KB
META-INF
MANIFEST.MF 39B
.classpath 841B
共 73 条
- 1
钱春华
- 粉丝: 141
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页