栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot上传、下载、批量导入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot上传、下载、批量导入

上传

未完成,明天继续
upload.html




    
    Title
    
    






UploadController.java

	@Controller
public class UploadController {
    @RequestMapping( "toUpload")
    public String toUpload(){
        return "upload";
    }
    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "E:\Git_Study\test01\src\main\resources\static\upload\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败!";
    }
}

下载

在写下载的时候,遇到了一个特别无语的问题,如果不是用的a标签,用按钮发送请求,那么只能设置按钮的window.location.href="下载路径",或者是window.open("下载路径")

  @RequestMapping("/download")
    @ResponseBody
    public String download(@RequestParam("fileName") String fileName, HttpServletRequest request, HttpServletResponse response){
        String filePath = prefixName+fileName; // 文件的路径
        System.out.println(filePath);
        File file = new File(filePath);
        if (!file.exists()){
            return "文件失效";
        }
        BufferedOutputStream outputStream = null;
        ServletOutputStream outputStreams  = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
            byte[] bytes = new byte[1024];
            int len = 0;
            response.reset();
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName,"UTF-8"));//文件名称
            outputStreams = response.getOutputStream();
            while ((len=inputStream.read(bytes))!=-1){
                outputStreams.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStreams.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return "下载成功";
    }

html
在线播放的东西,在这里使用对象的媒体标签就可以了,可以使用刚刚下载的那个接口。



忘情水 download1
download2
download3
批量导入
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号