java web 之springmvc教程(八)-----实现文件上传及下载

导入包:

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

单个文件的上传

前台页面index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<body>
<h2>Hello World!</h2>
<body>
<form action="${pageContext.request.contextPath }/doupload.action" method="post" enctype="multipart/form-data">
    <h2>文件上传</h2>
    文件:<input type="file" name="uploadFile"/><br/><br/>
    <input type="submit" value="上传"/>
</form>
</body>
</body>
</html>

springmvc.xml中添加:

<!-- 文件上传,需要配置MultipartResolver处理器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置船上文件的最大尺寸为5MB -->
        <property name="maxUploadSize" value="5242880"/>
        <property name="defaultEncoding" value="utf-8"/>
    </bean>

controller添加:

@RequestMapping("/doupload")
    public String doupload(MultipartFile uploadFile,HttpSession session) throws Exception{
        String filename = uploadFile.getOriginalFilename();
        String leftPath= session.getServletContext().getRealPath("/images");
        System.out.println("leftpath======" + leftPath);
        File file = new File(leftPath,filename);
        uploadFile.transferTo(file);
        return "/WEB-INF/jsp/success.jsp";
    }

先建立images文件夹。

springmvc文件上传的类是MultipartFile。uploadFile必须与jsp中的name一致。

多个文件的上传

index.jsp:

<form action="${pageContext.request.contextPath }/doupload2.action" method="post" enctype="multipart/form-data">
    <h2>文件上传</h2>
    文件:<input type="file" name="uploadFile2"/><br/>
    <input type="file" name="uploadFile2"/><br/>
    <input type="file" name="uploadFile2"/><br/><br/>
    <input type="submit" value="上传2"/>
</form>

controller:

@RequestMapping("/doupload2")
    public String doupload(@RequestParam MultipartFile[] uploadFile2,HttpSession session) throws Exception{
        for(MultipartFile item :uploadFile2){
            if(item.getSize()>0){
                String filename = item.getOriginalFilename();
                String leftPath= session.getServletContext().getRealPath("/images");
                File file = new File(leftPath,filename);
                item.transferTo(file);
            }else{
                return "/WEB-INF/jsp/error.jsp";
            }
        }

        return "/WEB-INF/jsp/success.jsp";
    }

测试成功。

文件下载

index.jsp

<a href="${pageContext.request.contextPath }/download.action?line.jpg">下载</a>

controller:

 @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        File file = new File("D:\\line.jpg");
        HttpHeaders headers = new HttpHeaders();
        String filename = new String("hello.jpg".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);
    }

测试成功!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值