spring mvc注解文件上传下载
需要两个包:
包如何导入就不介绍了,前端代码如下(一定要加enctype="multipart/form-data"让服务器知道是文件上传):
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="file" id="upimage" name="file" onchange="setImagePreview()"/> <input type="submit" value="Submit" /></form>
java代码:
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;
/**
* {@Controller} spring注解,指定此类为controller
* {@RequestMapping} 路径
* @author v_zweiwang
*
*/
@Controller
@RequestMapping("/image")
public class ImageController {
@RequestMapping("/toupload")//用于跳转到上传页面
public String toUpload(){
return "/WEB-INF/html/upload.html";
} @RequestMapping("/upload")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS"); //格式化时间
/**构建图片保存的目录**/
String upTime =dateformat.format(new Date()); //用于区别保存
String path=request.getSession().getServletContext().getRealPath("/");//获取项目在服务器的绝对路径file C:....webApp/ =url http://...webApp/ 不明白就打印path
String fileName = file.getOriginalFilename(); //获得文件名
String filePath="/upload/"+upTime+"/";//文件相对路径
File targetFile = new File(path+filePath,fileName); //新建一个文件
if(!targetFile.exists()){
targetFile.mkdirs();
} //保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(request.getContextPath()+filePath+fileName);
return "redirect:/image/download.do?filepath="+filePath+fileName;//直接重定向下载图片
} @RequestMapping("download")
public ResponseEntity<byte[]> download(HttpServletRequest request,String filepath) throws IOException {
String path=request.getSession().getServletContext().getRealPath("/")+filepath;//获取图片路径 filepath为图片相对路径
System.out.println(path);
File file=new File(path);
HttpHeaders headers = new HttpHeaders();
String fileName=new String("filescan.png".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName); //下载后显示的名字
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED); //向浏览器发送数据
}
}
spring mvc注解文件上传下载的更多相关文章
- spring mvc 实现文件上传下载
/** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...
- Spring MVC的文件上传和下载
简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...
- spring mvc ajaxfileupload文件上传返回json下载问题
问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...
- 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity
文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...
- Java Web 学习(8) —— Spring MVC 之文件上传与下载
Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...
- Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器
1.文件上传. spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...
- Spring MVC的文件上传
1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...
- Spring MVC实现文件上传
基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...
- 【Spring学习笔记-MVC-13】Spring MVC之文件上传
作者:ssslinppp 1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...
随机推荐
- 洛谷 P3527 [POI2011]MET-Meteors 解题报告
P3527 [POI2011]MET-Meteors 题意翻译 \(\tt{Byteotian \ Interstellar \ Union}\)有\(N\)个成员国.现在它发现了一颗新的星球,这颗星 ...
- 《Java程序设计》第六周学习总结 20165218 2017-2018-1
20165218 2017-2018-1 <Java程序设计>第六周学习总结 教材学习内容总结 第8章 常用实用类 String类 不可以有子类 在java.lang 包中,被默认引入 S ...
- AOJ.综合训练.2016-12-1
友情提示:不要复制粘贴,看完解析先自己尝试写一下,不行再看代码!祝AC愉快 @_@ A. 近似值计算 题意分析 根据公式,先用含有n的代数式表示出来pi,然后计算这个近似值和题目给出来的3.14159 ...
- linux下,手动切换jdk
1.首先将自定义的jdk目录安装到alternatives中 seven@ThinkPad:~/srcAndroid/src4..4_r1$ sudo update-alternatives --in ...
- 【树形DP】【P1364】医院放置
传送门 Description 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接 ...
- Web前端工程师-优秀简历汇总
Web前端工程师-优秀简历汇总 1. http://www.linqing07.com/resume.html 2. http://www.flqin.com/#page2 3. ht ...
- 让ie8、ie9支持媒体查询
<!-- 让IE8/9支持媒体查询,从而兼容栅格 --> <!--[if lt IE 9]> <script src="https://cdn.staticfi ...
- HDU 5533Dancing Stars on Me 基础几何
Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. Wh ...
- 2015/9/20 Python基础(16):类和实例
面向对象编程编程的发展已经从简单控制流中按步的指令序列进入到更有组织的方式中,依靠代码块可以形成命名子程序和完成既定的功能.结构化的或过程性编程可以让我们把程序组织成逻辑快,以便重复或重用.创造程序的 ...
- java collection(一)
1.Collection层次结构: 2.集合Conllection的基本概念: (1)集合的基本认识:如StringBuffer&StringBuilder是集合(存储的对象类型是String ...