Springboot上传、下载、批量导入

上传

未完成,明天继续
upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css"/>
    <script src="https://www.layuicdn.com/layui/layui.js"></script>
</head>
<body>
<button id="upload" class="layui-btn layui-btn-primary layui-border">原始按钮</button>
<script>
    layui.use(['layer', 'form','upload'], function () {
        var layer = layui.layer, form = layui.form, $ = layui.$ ,upload = layui.upload;
        $("#upload").click(function () {
            layer.open({
                type: 1,
                title: false,
                area: ['630px', '360px'],
                shade: 0.1,
                closeBtn: 0,
                shadeClose: true,
                btn: ['关闭'] ,
                content: '<div class="layui-upload-drag" id="test10">\n' +
                    '    <i class="layui-icon"></i>\n' +
                    '    <p>点击上传,或将文件拖拽到此处</p>\n' +
                    '</div>'
            });
            //拖拽上传
            upload.render({
                elem: '#test10',
                    accept: 'file', //普通文件
                    exts: 'xls|xlsx', //只允许上传excel文件
                url: '/upload' //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
                ,done: function(res){
                    console.log(res)
                    layer.msg(res);
                }
            });
        })

    });
</script>
</body>
</html>

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
在线播放的东西,在这里使用对象的媒体标签就可以了,可以使用刚刚下载的那个接口。

<audio src="download?fileName=忘情水.mp3" controls="controls">忘情水</audio>
<br/>
<a href="download?fileName=忘情水.mp3">忘情水</a>
<a href="/download?fileName=hbcallcenter.sql">download1</a><br/>
<a href="/download?fileName=test.xlsx">download2</a><br/>
<a href="/download?fileName=中文.txt">download3</a><br/>

批量导入

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值