SpringMVC上传和下载

根据狂神的SpringMVC中的上传和下载

1.文件上传

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

Jsp页面 必须有enctype=“multipart/form-data”

<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="upload">
</form>

文件上传配置文件

<!--  文件上传配置  -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--    请求编码格式    -->
        <property name="defaultEncoding" value="utf-8"/>
<!--    文件上传大小限制    10485760 = 10M -->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

Controller

第一种:复杂的

@Controller
public class FileController {
    @RequestMapping("/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //非空判断
        if("".equals(originalFilename)){
            return "redirect:/index.jsp";
        }
        System.out.println("上传文件名:"+originalFilename);
        //设置上传路径
        String path = request.getServletContext().getRealPath("/upload");
        //如果路径不存在,创建一个
        File realPath = new File(path);
        if(!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存路径:"+realPath);
        //文件输入流
        InputStream is = file.getInputStream();
        //文件输出流
        FileOutputStream os = new FileOutputStream(new File(realPath, originalFilename));
        //读取写入
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();

        return "redirect:/index.jsp";
    }
}

第二种:使用SpringMVC

@RequestMapping("/upload2")
public String upload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
    //上传路径保存设置
    String path = request.getServletContext().getRealPath("/upload");
    File realPath = new File(path);
    if (!realPath.exists()){
        realPath.mkdir();
    }
    //上传文件的路径
    System.out.println("上传文件的路径:"+realPath);
    //通过CommonsMultipartFile的方法直接写文件
    file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
    return "redirect:/index.jsp";
}

2.文件下载

@RequestMapping("/download")
public String down(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //下载图片的地址
    String path = request.getServletContext().getRealPath("/upload");
    //下载图片的名称
    String fileName = "wallhaven-3993m6.jpg";
    //设置响应头
    response.reset();//设置网页不缓存
    response.setCharacterEncoding("utf-8");//字符编码
    response.setContentType("multipart/form-data");//设置二进制传输数据
    response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
    File file = new File(path, fileName);
    FileInputStream input = new FileInputStream(file);
    ServletOutputStream out = response.getOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;
    while ((len = input.read(buff))!=-1){
        out.write(buff,0,len);
        out.flush();
    }
    out.close();
    input.close();
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值