需要两个包:

包如何导入就不介绍了,前端代码如下(一定要加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注解文件上传下载的更多相关文章

  1. spring mvc 实现文件上传下载

    /** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...

  2. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  3. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  4. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  5. Java Web 学习(8) —— Spring MVC 之文件上传与下载

    Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...

  6. Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器

    1.文件上传.      spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...

  7. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  8. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  9. 【Spring学习笔记-MVC-13】Spring MVC之文件上传

    作者:ssslinppp       1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...

随机推荐

  1. BZOJ1013:[JSOI2008]球形空间产生器——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1013 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在 ...

  2. 洛谷4577 & LOJ2521:[FJOI2018]领导集团问题——题解

    https://www.luogu.org/problemnew/show/P4577 https://loj.ac/problem/2521 参考:https://www.luogu.org/blo ...

  3. 阿里云遇到的坑:CentOS7防火墙(Firewalld),你关了吗?

    阿里云官方教程: https://help.aliyun.com/knowledge_detail/41317.html 百度参考的牛人教程(推荐): http://www.111cn.net/sys ...

  4. C语言函数的变参实用与分析

    实现变参传递的关键是: 传入参数在内存中是连续分布的. #define va_list void* #define va_arg(arg, type) *(type*)arg; arg = (char ...

  5. try catch finally 和return

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  6. 阿里C++研发实习二面和三面面经

    下午连着面了阿里爸爸的二面和三面,非常不明白别人的三面都是hr了,为什么我还是在技术面,难道面了个假阿里.不管怎么样,来篇面经攒攒人品. 二面 第一次遇到这么严肃的面试官,居然可以全程无表情的,面了这 ...

  7. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

  8. Sass 基本特性-基础 笔记

    一.变量声明 $ 变量的声明使用 $  所有的变量必须声明到变量调用之前 从3.4版本开始,Sass已经可以正确处理作用域的概念     在局部范围声明一个已经存在于全局内的变量时,局部变量就会成为全 ...

  9. Idea Ant 打开发包

    简介: http://ju.outofmemory.cn/entry/19239 语法: https://wenku.baidu.com/view/a0e00315866fb84ae45c8d9d.h ...

  10. PowerDesigner逆向工程

    再用PD建表完成后导成SQL脚本然后在SQL Server中运行后生成数据库后,就想到,可不可以将直接将数据库的内容生成PD文档?经过上网查,当然可以的. 要将SQL Server中的数据库导入到PD ...