【框架学习】SpringMVC上传下载文件

本文介绍如何使用Apache Commons IO和FileUpload库实现Web应用中的文件上传与下载功能。包括前端表单配置、后端文件处理及解决中文文件名乱码等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

web中上传下载就需要使用 apache的包,,

<commons-io.version>2.4</commons-io.version>
        <common-fileupload.version>1.3.1</common-fileupload.version>

      <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${common-fileupload.version}</version>
        </dependency>

使用maven,灰常方便,,(maven详解



一。上传文件

前端页面,部分代码

<form method="post" action="/student/upload_file" enctype="multipart/form-data" >
    <input type="file" name="file1"><br/>
    <input type="file" name="file2"><br/>
    <input type="submit" value="提交">
</form>

注意需要设置,enctype=”multipart/form-data”

后台接收代码:

   @RequestMapping("/upload_file")
    public String uploadFile(MultipartFile file1) throws IOException {
        File uploadFile = new File("/" + file1.getOriginalFilename());
        //将文件存入相应路径。
        file1.transferTo(uploadFile);
        return "show";
    }

很方便吧,此处只接受了一个文件,接受两个文件可在后面添加相应名称(名称与前端name对应),,出现重名,后台可以使用数组进行接收,,MultipartFile,方便的不能做朋友



二。下载文件
方式一:(有点错误,还未解决),有兴趣可以试试,很尴尬的错误,(多了两个双引号)

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        String path = "/download.txt";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        String fileName = new String("文本.txt".getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

ResponseEntity <byte[]>,泛型为byte[]时,会出现乱码,
ResponseEntity <String>,泛型为String时,会出两个冒号,文件还是有问题,



方式2,,完美下载,,亲测

   @RequestMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String realPath = "/download.txt";
        File file = new File(realPath);
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition",
                    "attachment;fileName=" + "/download.txt");// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

文件上传方式2,亲测完美

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鼠晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值