上传文件下载上传

上传

后端代码

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.UUID;

@Controller
public class TestUploadController {
    @RequestMapping("/toUpLoadPage")
    public String toPage(){
        return "/testUpload.html";
    }
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            try {
                /*
                 * 这段代码执行完毕之后,图片上传到了工程的跟路径; 大家自己扩散下思维,如果我们想把图片上传到
                 * d:/files大家是否能实现呢? 等等;
                 * 这里只是简单一个例子,请自行参考,融入到实际中可能需要大家自己做一些思考,比如: 1、文件路径; 2、文件名;
                 * 3、文件格式; 4、文件大小的限制;
                 */
                String filePath = "d:/files/"; //文件存放文件夹
                String fileName = file.getOriginalFilename();
                String path = filePath + fileName ;
                File upLoadFile = new File(path);
                FileOutputStream out = new FileOutputStream(upLoadFile);
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            return "上传成功";
        } else {
            return "上传失败,因为文件是空的.";
        }
    }
}

前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap 模板</title>
    <meta charset="UTF-8">
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
    <div class="form-group">
        <label for="file">文件输入1</label>
        <input type="file" id="file" name="file">
    </div>
    <button type="submit">提交</button>
</form>
</body>
</html>

下载

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Controller
public class TestDownLoadController {
    @RequestMapping("/downLoad")
    @ResponseBody
    public String downLoad(HttpServletResponse response) throws IOException {
        File file = new File("D:\\files\\123.xlsx");
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + "123.xlsx");// 设置文件名

        ServletOutputStream outputStream = response.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        byte[] bt = new byte[1024];
        int i = bufferedInputStream.read(bt);
        while (i != -1){
            outputStream.write(bt,0,i);
            i=bufferedInputStream.read(bt);
        }
        fileInputStream.close();
        bufferedInputStream.close();
        outputStream.close();
        return "下载成功";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值