mvc中file对象的上传和下载

2019-05-29  本文已影响0人  dwwl

两个应用场景:

springBoot中静态html js中存放的位置:resources下的static文件夹

json和multiPart对象同时作为参数接收(postman好像不能模拟这种请求)

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.js"></script>
</head>
<body>
<input type="file" id="file" name="file"/>
<button id="button" name="">上传</button>
</body>
<script>

    $(function () {
        $("#button").click(function () {
            //构建formData
            var formData = new FormData();
            //文件部分
            var file = document.getElementById("file").files[0];
            formData.append("file", file);
            //json部分
            var imageInfo = JSON.stringify({
                "width": "240",
                "height": "320"
            });
            //这里包装 可以直接转换成对象
            formData.append('imageInfo', new Blob([imageInfo], {type: "application/json"}));

            $.ajax({
                    url: "http://127.0.0.1:8001/test/upload",
                type: "post",
                //忽略contentType
                contentType: false,
                //取消序列换 formData本来就是序列化好的
                processData: false,
                dataType: "json",
                data: formData,
                success: function (response) {
                    alert(response);
                },
                error: function () {

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

实体类:ImageInfo.java

package com.huang.pojo;

public class ImageInfo {
    private String width;
    private String height;

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "ImageInfo{" +
                "width='" + width + '\'' +
                ", height='" + height + '\'' +
                '}';
    }
}

TestController.java

package com.huang.controller;

import com.huang.pojo.ImageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/test")
public class TestController {

    @PostMapping("/upload")
    public void upload(@RequestPart("file")MultipartFile file, @RequestPart("imageInfo")ImageInfo imageInfo) {
        System.out.println("file = " + file);
        System.out.println("imageInfo = " + imageInfo);
    }

}

文件下载 以及 下载文件名称的乱码问题

URLEncoder.encode

        response.setContentType(new MimetypesFileTypeMap().getContentType(tempZipFile));
        response.setHeader("Content-disposition", "attachment;filename="+          URLEncoder.encode(fileName, "UTF-8"));

        //返回文件字节数组
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(tempZipFile));
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[10240];
            int length = 0;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
上一篇下一篇

猜你喜欢

热点阅读