首页 > 经验记录 > SpringMVC文件上传下载

SpringMVC文件上传下载

Spring通过MultipartResolver实现文件上传的。
Spring上传组件定义在org.springframework.web.multipart包中。
 
默认情况下,Spring MVC上下文中没有装配MultipartResolver。如果要使用Spring的文件上传功能,需要先进行配置。
springmvc.xml中要加上这个

<!--文件上传解析器-->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
        <property name="maxUploadSize" value="2097152"/>
    </bean>

 
控制层方法:
我也记不住这东西,都是跟着教程敲得,为了有一天能用上,保留在这儿

package top.yibobo.controller;
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.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
@Controller
public class FileController {
    @RequestMapping("/upload")
    public String upload(MultipartFile myPic, HttpSession session) throws IOException {
        //获得保存文件目录的绝对路径
        String path = session.getServletContext().getRealPath("images");
        //取得原始文件名
        String fileName = myPic.getOriginalFilename();
        File file = new File(path,fileName);
        //转存文件到指定路径
        myPic.transferTo(file);
        session.setAttribute("fileName",fileName);
        return "suc";
    }
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String fileName,HttpSession session) throws IOException {
        //获得保存文件目录的绝对路径
        String path = session.getServletContext().getRealPath("images");
        //要下载的文件
        File file = new File(path,fileName);
        //响应头部对象数据
        HttpHeaders headers = new HttpHeaders();
        //定义响应内容类型为流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //通知浏览器以附件形式保存文件
        headers.setContentDispositionFormData("attachment",fileName);
        return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
    }
}

 
 
pom.xml中需要加上这个

<!--文件上传下载-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>

 
上传下载的jsp文件

<%--
  Created by IntelliJ IDEA.
  User: pyb
  Date: 2018/6/25
  Time: 13:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="commons/basePath.jsp"%>
    <title>上传</title>
    <form action="upload" method="post" enctype="multipart/form-data">
        <p>请选择上传文件<input type="file" name="myPic"></p>
        <p><input type="submit" value="提交"></p>
    </form>
</head>
<body>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: pyb
  Date: 2018/6/25
  Time: 14:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="commons/basePath.jsp"%>
    <title>tupian</title>
</head>
<body>
    <div>
        <img src="images/${fileName}">
        <p><a href="download?fileName=${fileName}">点击下载</a></p>
    </div>
</body>
</html>

 

           


EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00