SpringBoot文件上传于下载

导依赖

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

 配置最大上传文件大小

spring下:

 页面

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>
<p><a th:href="@{download/123.jpg}">下载图片</a></p>

上传

package com.qyc.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author qyc
 * @time 2020/4/21 - 17:45
 */
@Controller
public class Upload {
    @PostMapping("upload")
    public String upload(MultipartFile file) throws IOException {
        //文件信息
        System.out.println(file.getOriginalFilename()); //文件名
        System.out.println(file.getContentType());  //文件类型
        System.out.println(file.getSize());  //大小
        String realPath = ResourceUtils.getURL("classpath:").getPath()+"/static/file";
        String datafile = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        File files = new File(realPath,datafile);
        if(!files.exists()) files.mkdirs();
        //修改文件名
        String newfilename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+
                UUID.randomUUID().toString();
        //扩展名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String filename = newfilename +"."+ extension;
        file.transferTo(new File(files,filename));
        return "redirect:home";
    }
}

 

下载

package com.qyc.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author qyc
 * @time 2020/4/21 - 17:45
 */
@Controller
public class Download {
    @GetMapping("/download/{filename}")
    public void download(@PathVariable("filename") String filename, HttpServletResponse response) throws IOException {
        String realPath = ResourceUtils.getURL("classpath:").getPath()+"static/file";
        System.out.println(realPath);

        //读取文件
        File file = new File(realPath,filename);
        //文件输入流
        FileInputStream is = new FileInputStream(file);
        //    附件下载
        response.setHeader("content-disposition","attachment;fileName="+filename);
        //文件输出流
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(is,os);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值