package cn.edu.jseti.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
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;
import cn.edu.jseti.domain.User;
/**
* @author mm
* @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[] files) throws Exception {
System.out.println(description);
try {
for (MultipartFile file : files) {
//如果文件不为空,写入上传路径
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 + System.getProperty("file.separator") + filename));
}
}
} catch (Exception e) {
return "error";
}
return "success";
}
//单文件上传:上传文件会自动绑定到MultipartFile中
@RequestMapping(value="/register")
public String register(HttpServletRequest request,@ModelAttribute User user,Model model,@RequestParam("image") MultipartFile file) throws Exception {
System.out.println(user.getUsername());
//如果文件不为空,写入上传路径
if(!file.isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/images/");
//上传文件名
String filename = user.getImage().getOriginalFilename();
File f= new File(path+System.getProperty("file.separator")+filename);
//判断路径是否存在,如果不存在就创建一个
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
//将上传文件保存到一个目标文件当中
file.transferTo(f);
//将用户添加到model
model.addAttribute("user", user);
return "userInfo";
} else {
return "error";
}
}
//文件下载(方式1)
@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 : 二进制流数据(最常见的文件下载)。相当于Content-Type=[application/octet-stream]}
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
//文件下载(方式2)
@RequestMapping(value="/download2")
public static void download2(String filename,HttpServletRequest request,
HttpServletResponse response) throws Exception {
//设置响应头和客户端保存文件名
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + filename);
//下载文件路径
String filePath = request.getServletContext().getRealPath("/images/");
//打开本地文件流
InputStream is=null;
OutputStream os=null;
try {
is = new FileInputStream(filePath+ File.separator + filename);
//激活下载操作
os = response.getOutputStream();
//循环写入输出流
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e){
throw e;
}finally{
// 这里主要关闭。
os.close();
is.close();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
FileUpload.rar (47个子文件)
FileUpload
.project 1KB
WebContent
WEB-INF
content
userInfo.jsp 671B
error.jsp 367B
success.jsp 387B
registerForm.jsp 739B
uploadForm.jsp 835B
lib
commons-fileupload-1.3.1.jar 67KB
spring-jdbc-4.2.0.RELEASE.jar 412KB
spring-web-4.2.0.RELEASE.jar 748KB
spring-websocket-4.2.0.RELEASE.jar 418KB
spring-tx-4.2.0.RELEASE.jar 257KB
spring-aspects-4.2.0.RELEASE.jar 57KB
spring-beans-4.2.0.RELEASE.jar 712KB
spring-jms-4.2.0.RELEASE.jar 273KB
commons-logging-1.1.1.jar 59KB
spring-orm-4.2.0.RELEASE.jar 455KB
spring-instrument-4.2.0.RELEASE.jar 7KB
spring-expression-4.2.0.RELEASE.jar 256KB
javax.servlet-api-3.1.0.jar 94KB
spring-context-support-4.2.0.RELEASE.jar 176KB
spring-webmvc-4.2.0.RELEASE.jar 845KB
spring-instrument-tomcat-4.2.0.RELEASE.jar 10KB
spring-core-4.2.0.RELEASE.jar 1MB
spring-webmvc-portlet-4.2.0.RELEASE.jar 173KB
spring-context-4.2.0.RELEASE.jar 1.03MB
spring-aop-4.2.0.RELEASE.jar 355KB
commons-io-2.4.jar 181KB
spring-messaging-4.2.0.RELEASE.jar 362KB
spring-test-4.2.0.RELEASE.jar 537KB
spring-oxm-4.2.0.RELEASE.jar 81KB
springmvc-config.xml 2KB
web.xml 1KB
index.jsp 444B
META-INF
MANIFEST.MF 39B
src
cn
edu
jseti
domain
User.java 607B
controller
FileUploadController.java 6KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 140B
org.eclipse.wst.common.component 482B
.jsdtscope 567B
registerForm.jsp 733B
build
classes
cn
edu
jseti
domain
User.class 917B
controller
FileUploadController.class 6KB
.classpath 3KB
共 47 条
- 1
资源评论
- linyun662018-01-19可以实现单一文件与多文件上传,多谢分享
- 丿回到火星去2018-08-01不好 只是最简单的实现
外外外
- 粉丝: 19
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Cesium 高程为0的地形数据
- 国产化神通数据库-数据库开发手册
- Oracle数据库备份与恢复教程.zip
- 苹果闹铃音乐苹果闹铃音乐
- Unity导入Excel文件自动创建class文件和ScriptObject文件,并转化成ScriptObject的插件pro版
- UE4仅k位移产生完美行走动作-UE4项目
- 基于 Python 的校园导航系统
- tensorflow-2.6.3-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.6.2-cp39-cp39-win-amd64.whl
- tensorflow-2.6.2-cp39-cp39-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功